Attempt to execute rgb2gray script as a function [duplicate] - image

This question already has answers here:
Attempt to execute script as a function
(2 answers)
Closed 3 years ago.
I am doing image segmentation of round objects that have similar color to their background. The image is RGB but the RGB values give tones of gray, notice even though the image looks gray it is not in grayscale. In the segmentation process I have to apply gradient filters and opening and closing by reconstruction in order to separate the round objects from the background before making the binary mask that I will use for segmentation. Some of the functions I used in this process accept 2D arrays inputs only. In fact, binarization of the image itself can only be done in 2D array inputs, not in RGB images which are a 3D array. So, I am trying to convert my RGB image which looks gray but is not in grayscale first. But, I get the following error form MATLAB when I use the rgb2gray function:
Attempt to execute SCRIPT rgb2gray as a
function:C:\Users\Documents\MATLAB\rgb2gray.m
Error in Mask_Biophysics (line 2) Frame= rgb2gray(Frame1);
Does anyone what this error means and how to fix it?

You probably have created a script in C:\Users\Documents\MATLAB named rgb2gray.m which hides the default implementation of rgb2gray. Try removing or renaming this script file and run your program again. It is always a good idea to avoid giving files the same name as built-in MATLAB commands.

Related

Upscale an image but making it look the same [duplicate]

This question already has answers here:
Enlarge image pixels with opencv
(2 answers)
Closed 2 years ago.
I have an image that is 8x8px, and i need it to be 16x16, but I want it too look the exact same. So I want every pixel in the 8x8 version to be 4 pixels, making it 16x16. Is there a way to do this?
I want every pixel in the 8x8 version to be 4 pixels, making it 16x16.
What you are describing is commonly called "nearest neighbor" resampling. Most image editing software has an option for this.
One way to do factor-2 nearest neighbor upscaling is to use the ImageMagick convert command line program like:
convert in8x8.png -scale 200% out16x16.png
Or if you have MATLAB and the Image Processing Toolbox, you can do it with imresize as imresize(I, 2, 'nearest').

imshow shows image as noisy even though its not in windows image viewer

Im trying to learn a segmentation network using CNN, my network is producing very poor results. I looked at the images and I'm wondering if this is the reason. My input images are stack of images in a .tif file. In Windows image viewer below is what I get:
I am trying to detect and segment the bright spots shown above
But when I open the same in Matlab using imshow() I get
All the information is basically lost. However when I use imagesc() I get the following:
Which is much better, but why are my images not working with my network? I am getting very very unpredictable losses and accuracy even with tried and tested networks.
Is it because my image is reading in the version shown in imshow()?
In MATLAB, the following convention is used for images:
uint8: pixels are in the range [0,255].
double: pixels are in the range [0,1].
When using imshow on a double image, values between 0 and 1 are mapped to a color scale (typically black to white). Any value above 1 is also mapped to white. This is what is happening to you: most of your pixels are shown as white.
It is likely that the CNN you are using makes the same assumptions and therefore clips your data.
The solution is to properly scale your images when you read them in. See for example im2double.
Neither imshow nor imagesc is designed to handle tiff stacks. They are for viewing, not reading image data. You may see a warning along the lines of the following, also:
"Can only display one frame from this multiframe file"
You can use imread to read in each of the frames in the file separately, as per this answer, or tiff which is a Matlab gateway to LibTiff library routines and provides more detailed control of how you read in your images if imread doesn't hack it.

Matlab subplot shows tiny images

I have an algorithm that does a set of 8 image processing operations in an input image and then I want to show the output of each of them in a grid of 8 images. The problem is that I want to show image by image after each of the operations ends. By using subplot and imshow the image outputs I want to show in the grid are small.
Here is some piece of my code
output1=image_operation(input_image);
subplot(4,4,1);
imshow(output1);
I have heard about imdisp and montage functions, but they don't do what I want. I want to show the first image when the first algorithm ends, then the second image together with the first when the second algorithm ends and so on. What these functions do is showing all images at once and I don't want this.
Is there anything I missed?
The reason why your output images are too small is that you want to plot 8 images but you're using subplot command for 16 images i.e. subplot(4,4,x). Since you want to plot 8 images using subplot, use any of following instead:
subplot(4,2,x)
or subplot(2,4,x)

Reading voxel values from binary file into matlab

I have a 16bit voxel data set from which I need to extract the integer values for each voxel. The data set can be downloaded from here, it is the 'Head Aneuyrism 16Bits' data set (You need to click on the blood vessels image to download the 16bit version). Its size is 512x512x512, but I don't know whether it is greyscale or color, nor if that matters. Looking at the image on the website I'd guess that it is color, but I am not sure whether the image should be taken literally.
A related question on SO is the following: How can I read in a RAW image in MATLAB?
and the following on mathworks: http://www.mathworks.com/matlabcentral/answers/63311-how-to-read-an-n-dimensioned-matrix-from-a-binary-file
Thanks to the information in the answers to these questions I managed to extract some information from the file with matlab as follows:
fileID=fopen('vertebra16.raw','r');
A=fread(fileID,512*512*512,'int16');
B=reshape(A,[512 512 512]);
I don't need to visualise the image, I only need to have the integer values for each voxel, but I am not sure whether I am reading the information in the correct way with my script.
The only way I found to try and check whether I have the correct voxel values is to visualise B using the following:
implay(B)
Now, with the code above, and then using implay(B) I get a black and white movie with a white disc in the center and black background and some black pixels moving in the disc (I tried to upload a frame of the movie, but it didn't work). Looking at the image on the website from which I downloaded the file, the movie frames I get seem quite different from that image, so I'd conclude that I do not have the correct voxel values.
Here are some questions related to my problem:
Do I need to know whether the image is in grey scale or color to read the voxel values correctly?
On the data set website there is only written that the data set is in 16bit format, so how do I know whether I am dealing with signed or unsigned integers?
In the SO question linked to above they use 'uint8=>uint8'. I could not find this in the matlab manual, so I wonder whether 'uint8=>uint8' is an obsolete matlab notation for 'uint8' or if it does something different. I suspect that it does something different since if I use 'int16=>int16' instead of 'int16' in my code above I get a completely black movie with implay.
It looks like you read the data correctly.
The problem when displaying it is the scale of the values. implay seems to assume the values to be in [0,1] and therefore clamps all values to be in that range, where are your data range is [0,3000].
Simply doing
B = B / max(B(:))
will rescale your data to [0,1] and looking at the data again with
implay(B)
shows you something much more sensible.

Ruby: create an image with RGB values

I have a series of RGB values I'm working with in Ruby, and I'd like to take those and output an image from them. I've done the processing with RMagick, but it consumes a ton of memory so I was hoping to profile that against other alternatives. Right now I'm just getting the image pixels using the ImageMagick command line convert option to get the pixel data, but I can't seem to figure out how to write the data back out to a new image. How do I take an array of RGB values and write them into a png or jpg image on the filesystem?

Resources