plot3d of an image in SCILAB - matlab

I'm try to plot a gray image using plot3d in Scilab, here is my code:
clear;
clc;
direc='C:\Users\engine\Documents\EYE';
cd(direc)
stacksize('max');
fileName = '50.jpg';
rgb= ReadImage(fileName);
img = RGB2Gray(rgb);
Histogram = CreateHistogram(img);
figure(1); plot( 0:255,Histogram);
plot3d(1:length(img(:,1)),1:length(img(1,:)),img(:,:));
imshow(img);
When I run this code, I get this error message :
!--error 246
Function not defined for given argument type(s),
check arguments or define function %i_plot3d for overloading.
Any idea how can I solve this?

I found the error. I should convert the gray image to double values, the plot works fine then :
clear;
clc;
direc='C:\Users\engine\Documents\EYE';
cd(direc)
stacksize('max');
fileName = '50.jpg';
rgb= ReadImage(fileName);
img = RGB2Gray(rgb);
img2 = im2double(img);
img2 =img2*250;
plot3d(1:1024,1:1280,img2(:,:));

Related

How do I convert a DICOM file into RGB?

I'll explain my problem in Matlab. I have a multi-frame DICOM file, 70x70x50. I would like to convert it to RGB.
This is my code:
clear all;
close all;
clc;
% Lettura Dicom
img = dicomread('provaDICOM.dcm'); % 4-D int16
info = dicominfo('provaDICOM.dcm');
img2 = squeeze(img); % 70x70x50 int16
img3 = mat2gray(img2); % 70x70x50 double
% volumeViewer(img2);
% Conversione RGB
cmap = copper(256);
numslice = size(img3,3);
colored_MHA = zeros(size(img3, 1), size(img3, 2), numslice, 3);
for slice = 1 : numslice
colored_MHA(:,:,slice) = ind2rgb(img3(:,:,slice), cmap);
end
This is the error:
Unable to perform assignment because the size of the left side is 70-by-70 and the size of the right side is
70-by-70-by-3.
Error in Dicom (line 18)
colored_MHA(:,:,slice) = ind2rgb(img3(:,:,slice), cmap);
You need to reference an extra dimension on colored_MHA.
Try:
colored_MHA(:,:,:,slice) = ind2rgb(img3(:,:,slice), cmap);
More generally - the error is telling you that you're trying to put something big into something too small, and when you see this you can ask yourself why that is.

Error when using imellipse to create mask

I try to create mask on a image, however I get the following error:
Undefined function 'createMask' for input arguments of type
'matlab.graphics.primitive.Image'.
I can't figure out what's wrong. Any help? Thanks in advance!
img = imread('pout.tif');
figure, h_im = imshow(img);
h = imellipse;
position = wait(h);
BW = createMask(position,h_im);
Solution
If I understand correctly, you are trying to get an ellipse input from the user and obtaining a mask of it.
There are two changes which needs to be done:
The wait function is not needed, since imellipse function waits until the user provide an ellipse.
First argument of createMask should be the ellipse h.
Updated code
img = imread('pout.tif');
figure, h_im = imshow(img);
h = imellipse;
BW = createMask(h,h_im);
Result

error while masking an image

I just want to threshold it before applying algorithm.i tried it in one algorithm and got perfect result.but here i considered binary image and then masking.i just want the intervertebral disc(ellipse portion) which is in between vertebrae .can you suggest any other method?
close all;
clear all;
%%
Img = imread('Sub.png');
Img=rgb2gray(Img);%convert to single channel as data do not contain color image
Img=im2bw(Img,.16);
fig1=figure;
subplot(221)
imshow(Img); title('Original Image')
m1 = uint8(Img>35);
subplot(222), imshow(m1,[]); title('Mask m1');
m2=uint8(Img<300);
subplot(223), imshow(m2,[]); title('Mask m2');
sd = stdfilt( double(Img), ones(3,3));
m3= uint8(sd<18);
subplot(224), imshow(m3,[]); title('Mask m3');
fig2=figure;
I = uint8(Img.* m1);
I = uint8(I.* m2);
I = uint8(I.* m3);
subplot(221), imshow(I);
title('Masked Img')
i'm getting error as
I cannot view mask1 , mask 2, mask3 and also I'm getting error
Error using .*
Integers can only be combined with integers of the same class, or scalar doubles.
Error in test1 (line 22)
corrected code is
close all;
clear all;
%%
Img = imread('Sub.png');
Img=rgb2gray(Img);%convert to single channel as data do not contain color image
Img=im2bw(Img,.16);
fig1=figure;
subplot(221)
imshow(Img); title('Original Image')
m1 = uint8(Img>35);
subplot(222), imshow(m1,[]); title('Mask m1');
m2=uint8(Img<300);
subplot(223), imshow(m2,[]); title('Mask m2');
sd = stdfilt( double(Img), ones(3,3));
m3= uint8(sd<18);
subplot(224), imshow(m3,[]); title('Mask m3');
fig2=figure;
Img(~m1)=0;
Img(~m2)=0;
Img(~m3)=0;
subplot(221), imshow(Img);
title('Masked Img')
There are probably multiple issues with the code. While applying a mask using multiplication is technically possible, I recommend to use indexing instead:
Img(~mask)=0;
This code sets every pixel which is not in mask to zero.
There is probably another issue with your code. You are using uint8 to get a uint8 image, but the function does not rescale. If you input a normalized double image (values between 0 and 1) you will end up with a black image because it contains only 0 and 1 values. Use im2uint8 to convert images, but I think what you are trying to do here could be done on the double image as well.

error while drawing several x-marks on a binary image in matlab

I am trying to draw several x-marks on a binary image to some co-ordinates.
My output should look like this
To do this i have written some codes below
clear all;
% Here four co-ordinates are provided as an example. In my main code the co- ordinates will be hundred or thousands
position{1}.x = 10;
position{1}.y = 10;
position{2}.x = 20;
position{2}.y = 20;
position{3}.x = 30;
position{3}.y = 30;
position{4}.x = 40;
position{4}.y = 40;
% Read image as binary
image = imread('test34.jpg');
figure('name','Main Image'),imshow(image);
gray_image=rgb2gray(image);
level = graythresh(gray_image);
binary_image = im2bw(image,level);
% Define color of the x-marker and initializing shapes which i want to draw
red = uint8([255 0 0]);
markerInserter = vision.MarkerInserter('Shape','X-mark','BorderColor','Custom','CustomBorderColor',red);
i = 1;
% The loop will continue to the number of co-ordinates which will never be predefined in my main code
while i<=numel(position)
% Converting binary to RGB for only once.
if i == 1
rgb = repmat(binary_image, [1, 1, 3]);
else
rgb = repmat(binary_image, [1, 1, 1]);
end
% Position where x-marks will be drawn
Pts = int32([position{i}.x position{i}.y]);
% Draw x-marks
binary_image = step(markerInserter, rgb, Pts);
i = i+1;
end
figure('name','binary_image'),imshow(binary_image);
But i am getting these errors
Error using images.internal.imageDisplayValidateParams>validateCData
(line 119)
If input is logical (binary), it must be two-dimensional.
Error in images.internal.imageDisplayValidateParams (line 27)
common_args.CData = validateCData(common_args.CData,image_type);
Error in images.internal.imageDisplayParseInputs (line 78)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 227)
[common_args,specific_args] = ...
Error in test2 (line 39)
figure('name','binary_image'),imshow(binary_image);
I have tried some examples but they are experimented on RGB image while i need it on binary image and that's where i am getting errors.
Why i am getting these errors and what will be the solution?
Please provide explanation with proper code.
You code seems a bit complicated (to me) for what it is intended. Here is a simple example demonstrating how to draw red crosses using scatter on a binary image. As you see, the image in itself is binary and the markers are in color, so the 'net' image is no longer binary... Also, there is no need to convert your RGB image to graylevels before applying im2bw.
And don't forget to use hold on to avoid discarding the image when adding the markers!
Here is the code with comments:
clear
clc
close all
%// Read image
MyImage = imread('peppers.png');
%// Convert to binary. No need to use rgb2gray.
MyBWImage = im2bw(MyImage(:,:,2));
%// Define some x- and y positions for markers
xpositions = linspace(10,300,40);
ypositions = linspace(20,500,40);
imshow(MyBWImage)
%// IMPORTANT!!
hold on
%// Draw markers
scatter(xpositions.',ypositions.',80,'r','x')
With output:
Is this what you meant?

problems with imshow() and rgb2gray() in MATLAB

Good afternoon,
when running the following code:
testImage = double(imread(testfile));
figure; imshow(testImage)
greyTestImage = rgb2gray(testImage);
figure; imshow(greyTestImage)
I get unclear and mostly blank Images, I was unable to fx it using the colormap. The following is teh original image and the two resulting figures:
1:
2:
3:
You have to know the format of the image you are trying to read. To make this sure, I always use the following piece of code in my programs when I want to convert user defined image from unknown format to uint8 grayscale :
% load image
[filename, pathname] = uigetfile({'*.*'},'image file');
fullFilename = [pathname filename];
% Get image info, read it accordingly
info = imfinfo(fullFilename);
if(strcmp('truecolor',info.ColorType))
I = imread(fullFilename);
Igray = uint8(rgb2gray(I));
clear I
elseif(strcmp('grayscale',info.ColorType))
Igray = uint8(imread(fullFilename));
elseif(strcmp('indexed',info.ColorType))
[I,map] = imread(fullFilename);
Igray = uint8(ind2gray(I,map));
clear I map
else
error('statPart:FormatImage','Image format error');
end
clear info
Also, this : testImage = double(imread(testfile)); won't work if you assume testfile is uint8 and want to convert it to double (in double intensity shall range from 0 to 1). You have to do testImage = double(imread(testfile)) / 255;
Hope this help.
Cheers
Have you tried converting to 8 bit unsigned integers?
testImage = double(imread('PEYXW.jpg'));
figure; imshow(uint8(testImage))
greyTestImage = rgb2gray(uint8(testImage));
figure; imshow(greyTestImage)
This is probably an issue with the image you are loading.
The input to the rgb2gray function needs to be a 3-channel color image (MxNx3 matrix for the R,G,B channels).
If you use a built-in Matlab sample image, you can effectively do:
imdata = imread('ngc6543a.jpg');
figure; imshow(imdata)
Then:
test = rgb2gray(imdata);
Actually, Matlab provides the function Im2double instead of double to convert images to double precison:
testImage = im2double(imread(testfile));
figure; imshow(testImage)
greyTestImage = rgb2gray(testImage);
figure; imshow(greyTestImage)
In double precision, image RGB values are between 0 and 1, for uint8 they range between 0 and 127. When you use the double function, all RGB values keep their value, but they are in double precision. This means that any RBG tiple with values >= 1 will result in a double >= 1 and thus produce a white space in your image.
im2doubleactually scales the values to the range [0 1].