I have a couple hundred images like this one:
They are in a single folder and am trying to figure out a way in Matlab to automatically analyze each image's pixel values at point (700,755). I know how to do this one-at-a-time, as such:
rgb=impixel(p,700,755)
This returns the red-green-blue values at that particular point for the image. I'm very new to Matlab...what piece of code would analyze each image in the folder and save the RGB value on separate lines in a table/array?
Also, I have selected an area using the pixel region tool: '[696.463836477986 750.095011390851 19.9889937106933 13.3672527600921]' How do I analyze all the pixel values in that area and get the statistics (min, max, mean, etc.)...plus do that for all 200 images I have in the folder?
I appreciate the help!
AP
impixel can work for all the image as well:
impixel(I)
or for a specific (pixel) column and row:
impixel(I,c,r)
But you need first to read the image into a matrix. The imread function, returns all the RGB data of an image in an array:
A = imread(filename, fmt)
it reads a grayscale or color image from the file specified by the string filename. A is an array containing the image data. If the file contains a 'grayscale' image, A is an 'M-by-N' array. If the file contains a 'truecolor' image, A is an 'M-by-N-by-3' array. (3: R-G-B)
To read a bunch of files in a folder do this:
files = dir('*.jpg');
for i=1:length(files)
eval(['imread ' files(i).name]);
end
You can use imcrop function to crop the images you have:
using mouse:
I2 = imcrop(I)
or using dimensions:
I2 = imcrop(I,[75 68 130 112]);
Related
I need help in converting a png image back to the original values calculated in MATLAB. I originally downloaded a RADAR image and used xlim & ylim to to plot a zoomed area, then I used the command line (saveas(gca, [opath 'VS',num2str(k),'L',num2str(LL)], 'png')) to save the image to a png file. Now, I am trying to take the png file and convert it back to the original pre-saved matrix. The problem is I do not get the exact values back, as I originally had. I have a couple of colleagues who have also tried and we cannot get the original numbers. Can anyone help? I can provide a pre-saved matrix, lat & lon file (1832 x 720), the plot routine that I use xlim & ylim, the save line, then the routine that takes the png file and tries to return it to the original pre-saved plotted size.
for i=1%:length(tt)
imgpath2=[imgpath tt(i).name];
map=colormap(jet(256));
RGB=imread(imgpath2);
S=rgb2ind(RGB,map);
S=double(S);
L=size(map,1);
out=interp1(1:L,linspace(min_org,max_org,L),S(:,:));
%Turns white spaces into NaNs
out(out==27.176470588235293)=NaN;
% pixels around campus
ave=nanmean(nanmean(out(cam_wid,cam_len)));
testR=out(cam_wid,cam_len);
testR2=10.^(testR./10);
br=nanmean(testR2(:));
rr(i)=(br/300)^(1/1);
end
I have this image which with point curse it shows
X ,Y
index
RGB
the index values are important for me and I want to save this image and use it later. but when I am using
imwrite(im, 'importantIm.png' , 'png')
the created image does not have index value. here is the resulting saved image (png format);
I produce im , like below
im=zeros([1204 1204]);
with a loop add data to it.
I have a image and some contours as bellow figure. I want to save the output into image (png or jpg). The saved image only contains the image region without the matlab window. Let see my example in the figure. Could you have me implement it by matlab? This is my code to make output figure
img = imread('coins.png');
mask_red=zeros(size(img));
mask_green=zeros(size(img));
mask_red(30:160,40:170)=1;
mask_green(70:100,60:130)=1;
imagesc(uint8(img),[0 255]),colormap(gray),axis off;axis equal,
hold on;
[c1,h1] = contour(mask_red,[0 0],'r','Linewidth',3);
[c2,h2] = contour(mask_green,[0 0],'g','Linewidth',3);
hold off;
%% Save output figure
Use the getframe and cdata idiom. If the figure is open, simply do this:
f = getframe;
im = f.cdata;
im will contain the image that was contained inside your frame as a RGB image. Running your code in your post, then the above code, then doing imshow(im), we get:
If you want to save the image, just use imwrite:
imwrite(im, 'coins_final.png');
The image will be saved in a file called coins_final.png.
I have grayscale satellite image which is processed from spectral data (band classifications). If i use jet colormap in imshow it will show absolute colormapped image. But if i try to imwrite in particular place it is saved like a bluish image. I saw one example in matlab central, but i didnt get. can anyone help me to write my image with colorscaled image.
Matlab central link: http://www.mathworks.in/matlabcentral/answers/25026-saving-grayscale-image-as-it-appears-in-jet-colormap-of-imagesc
there accepted answer link is : http://www.mathworks.com/matlabcentral/fileexchange/7943
I have tried many times, this will show colormaped images in plots (imshow) they didnt write anywhere with colormaped. Now i want to write my image with colormaped.
example code:
I= imread('image path');
imshow(I,'colormap',jet);
imwrite(I,'path','jpg'); /not working
or
imwrite(I,jet,'path','jpg'); /not working
Please help to solve this issue.
When you use imshow the colormap is always adjusted to the range of values in your image. imwrite however assumes your image has a value range of [0,1] if you are using single or double data types. Try to scale your image to the range [0,1] before saving.
If you provide a colormap in the call to imwrite, MATLAB assumes you are using an indexed image. Thus you will have to convert the image to the indexed format first. The following snippet worked for a test image I of mine:
% scale to [0,1]
I = I - min(I(:));
I = I ./ max(I(:));
% Create indexed image
[J,~] = gray2ind(I);
% Save image
imwrite(J,jet,'path','jpg');
Solution by hbaderts worked well for me, but later I found out that some images were still scaled slightly different way from imshow.
However, I might found a reason of an original problem. Just after Matlab starts, its default colormaps (including 'jet') are set to 64 colors (64x3). Then, if any image is shown with a colormap, for example if imshow('cameraman.tif'), colormap('jet') is executed, all default colormaps become 256x3 (can be verified with jetMap=jet; before and after). Then it might happen that an image was written with a colormap different from the one applied to image figure (for example, if a figure called after imwrite).
Finally I found this solution (no image pre-scaling needed):
% Create indexed image, explicitly using 256 colors
imInd=gray2ind(im,256);
% Convert indexed image to RGB using 256-colors jet map
jetRGB=ind2rgb(imInd,jet(256));
% Save image
imwrite(jetRGB,'jet.png');
The images I used have the same color scale now, both the saved one and the one shown in figure.
I used these codes to produce an RGB image with gray levels between 50 and 170.
a='C:\Users\sepideh\Desktop\IP_abadpour\S45C-113050518040.jpg';
b=imread(a);
b=b+50;
b(b>170)=170;
and you'll see when I call functions max and min, it is proved that the gray levels are between 50 and 170.
max(max(max(b)))
ans =
170
min(min(min(b)))
ans =
50
then I used imshow and imsave functions to save the image with the name "50to170"
c=imshow(b);
d=imsave(c);
Now I read the written image in this way:
a='C:\Users\sepideh\Desktop\IP_abadpour\50to170.jpg';
b=imread(a);
This time when I call max and min functions,I see:
max(max(max(b)))
ans =
235
min(min(min(b)))
ans =
16
I mean it seems that gray levels have been changed after using imshow and imsave functions!
Why does it happen?
Is it because of the format (.jpg) that I'm using when employing imsave function?
Instead of using imsave, use imwrite
b=imread(a);
b=b+50;
b(b>170)=170;
imwrite(b,'50to170.png','png')
Notice that I am saving it as a png file instead of a jpg to prevent compression. Bitmap also saves it without compression.
This method is a more direct way to save raw image matrices than using imshow and imsave.
If you want the same functionality of imsave (selecting where the file goes) check out "uiputfile" to get file name and location.
I tested it.
1.First of all you can't use imsave(b) because the function imsave expects its first input argument to be a valid handle to a single graphics object.So first you should show the image in form c=imshow(b) and then use c as an input for function imsave.
2.If you want to save the image without showing it first use function "imwrite" and pass b as the first input argument to it.
Note that if you don't want gray levels be changed after saving, you should use 'bmp' as the save format not 'jpg'.
Because 'jpg' does not support indexed images and "imwrite" converts indexed images to RGB before writing data to JPEG files so the gray levels might be changed.It's not related to the contrast stretching when showing the image in matlab.It's related to the format used to save the image.