for loop causing bad text segmentation in matlab - image

the input images are a.jpg and b.jpg
these two image stored in for example comp folder.and want to write the segmented image in segment folder.but I think for looping problem segmentation repeated for so many times for each image.And I could't solve the problem.
here is my code
Resultado='C:\Users\Nurul\Desktop\picsegment';
srcFiles = dir('C:\Users\Nurul\Desktop\comp\*.jpg');
for i = 1 : length(srcFiles)
filename = strcat('C:\Users\Nurul\Desktop\comp\',srcFiles(i).name);
a = imread(filename);
LLL=a;
s=regionprops(LLL);
figure,imshow(LLL); title('segmented Image');
hold on
for J=1:numel(s)
rectangle('Position',s(J).BoundingBox,'edgecolor','g')
end
im1=LLL;
baseFileName = sprintf('%d.jpg', i); % e.g. "1.png"
fullFileName = fullfile(Resultado, baseFileName);
imwrite(im1, fullFileName);
end
plz help
thanks

You are saving your data as jpg, big mistake!
Still, if you want to keep the data saved as jpg, remember that it will not be saved as a binary image, and that means you need to binarize it again! Otherwise, every little pixel noise will be detected as data by regionprops, thats why you get so many squares.
Just add
a = imread(filename);
a=im2bw(a,0.5); % Add this line. The fancy way would be im2bw(a,graythresh(a)), but 0.5 will do in your case
LLL=a;

Related

Rice grain Segmentation in Matlab

I have an image of multiple rice grains in jpg format. I need to separate the individual rice grains into a separate images.
The input image is as below:
The resultant images should contain individual rice as below:
Currently I am using the following code:
close all;
BW = imread('img11_Inp','jpg');
L = bwlabel(BW)
figure
imshow(BW);
CC = bwconncomp(L);
stats = regionprops(L,'Image');
stats
%Display the first component as an image
Image1 = stats(2).Image
figure
imshow(Image1);
There are two problems with this code. Firstly, it is showing only two images rice grains in the "stats" structure and secondly it is also showing some noise. Please let me know what is the problem with this code
Look at this code with the comments
close all;
clear
I = imread('rice.jpg'); % jpg add noise. TODO: use png format
level = graythresh(I); % Global image threshold using Otsu's method
BW1 = imbinarize(I,level); % need to threshold, jpg add low level noise.
BW2 = imfill(BW1,'holes'); %fill holes inside grains
[L,n] = bwlabel(BW2); % label connected components
B = labeloverlay(I,L);
figure('Name',sprintf('NLables=%d',n)); imshow(B);
stats = regionprops(L,{'Centroid','Area'});
for i=1:length(stats)
x=stats.Centroid(i,1);
y=stats.Centroid(i,2);
area=stats.Area(i);
text(x,y,sprintf('Area=%d',area),'FontSize',8);
end

Reading & Writing Grayscale TIF files in Matlab

I'm trying to extract a (very) large number of subimages from a large grayscale TIF file and save each image as a GIF, PNG, or even another TIF using MATLAB. I'm able to display the individual images using imshow(sub(:,:,1),cmap) but when I try to write the data to an image file, the generated files are just white squares 101x101 px. Using the cmap argument in imwrite produces the same result, as does changing the image format (I've tried with PNG, TIF, GIF, and JPG with no luck). The file a.tif is 16 bit according to the property menu in Windows Explorer. Any help is appreciated. I'm really at wit's end with this.
% Import coordinates array and correct for multiplication by 10
datafile = 'data.xlsx';
coords = xlsread(datafile,1,'G2:H13057');
x = coords(:,1) ./ 10;
y = coords(:,2) ./ 10;
r = 50;
[img, cmap] = imread('a.tif'); % import the image
s = 2*r+1; % scalar of size of each submatrix in the array (sise of image)
sub = zeros(s,s,num); % create 3D matrix/array of matrices. Each submatrix corresponds to 50 px box around each point
i = 1:4;
subrgb = zeros(s,s,num);
for i=1:4
sub(:,:,i) = img((y(i)-r):(y(i)+r),(x(i)-r):(x(i)+r));
filename = 'dot_%d.png';
filename = sprintf(filename,i);
imwrite(sub(:,:,i),filename,'png');
end
Try changing the line:
sub = zeros(s,s,num);
to:
sub = zeros(s,s,num,class(img));
I assume that the problem is that sub is of type double.
Good luck

How do I save an image generated by imshow(image) into a variable?

This is my code. I want to save the image displayed on imshow(img) into a variable to use it later. Thanks!
img=imread('image1.bmp');
figure(1), imshow(img);
[r c]=ginput(4);
Bw=roipoly(img,r,c);
% figure,imshow(Bw)
[R C]=size(Bw);
for i=1:R
for j=1:C
if Bw(i,j)==1
img(i,j)=img(i,j);
else
img(i,j)=0;
end
end
end
figure,
imshow(img); title ('Output Image');
You can use the classic getframe / cdata idiom. With the figure window open, simply do this:
figure;
imshow(img); title('Output Image');
h = getframe;
im = h.cdata;
h is a handle to the current frame that is open, and the cdata field contains image data for the frame. The above code stores the frame image data into a variable called im for use for later.
Minor Comment
That for loop code to set the output is a bit inefficient. You can do this completely vectorized and you'll notice significant speedups.
This code:
for i=1:R
for j=1:C
if Bw(i,j)==1
img(i,j)=img(i,j);
else
img(i,j)=0;
end
end
end
... can be replaced with:
img(~BW) = 0;
I also don't understand why you'd need to store the image data inside the frame of imshow... when img already contains your data and you are ultimately showing the data contained in img. Why can't you just use img directly for your application? Nevertheless, the above solution will work.

How to make gif images from a set of images in matlab?

How to make '.gif' image from a set of '.jpg' images (say: I1.jpg, I2.jpg,..., I10.jpg) in matlab?
Ok here is a simple example. I got an image with a unicorn on it and remove 2 part to create 3 different images, just for the sake of creating an animated gif. Here is what it looks like:
clear
clc
%// Image source: http:\\giantbomb.com
A = rgb2gray(imread('Unicorn1.jpg'));
B = rgb2gray(imread('Unicorn2.jpg'));
C = rgb2gray(imread('Unicorn3.jpg'));
ImageCell = {A;B;C};
figure;
subplot(131)
imshow(A)
subplot(132)
imshow(B)
subplot(133)
imshow(C)
%// Just to show what the images look like (I removed spots to make sure there was an animation created):
%// Create file name.
FileName = 'UnicornAnimation.gif';
for k = 1:numel(ImageCell)
if k ==1
%// For 1st image, start the 'LoopCount'.
imwrite(ImageCell{k},FileName,'gif','LoopCount',Inf,'DelayTime',1);
else
imwrite(ImageCell{k},FileName,'gif','WriteMode','append','DelayTime',1);
end
end
As you see, its not that different from the example on the Mathworks website. Here my images are in a cell array but yours might be in a regular array or something else.That should work fine; when I open 'UnicornAnimation.gif' it is indeed a nice animation!
Hope that helps!

Matlab: How can I display several outputs in the same image?

Let's say my image is img=zeros(100,100,3), my outputs are several ellipse which i get using a created function [ret]=draw_ellipse(x,y,a,b,angle,color,img), I can display one ellipse using imshow(ret).For the moment, I'm trying to show serval ellipse in the image. But i don't know how to code it. will ‘for loop’ work or I need to hold them?
If this is related to what you were doing in your previous question, then what you need to do is to pass the result of one iteration as input to the next.
So assuming that the function [ret]=draw_ellipse(x,y,a,b,angle,color,img) you mentioned takes as input an image img and returns the same image with an ellipse drawn on it, you could do this:
%# ellipses parameters
%#x = {..}; y = {..};
%#a = {..}; b = {..};
%#angle = {..}; color = {..};
img = zeros(200,100,'uint8'); %# image to start with
for i=1:10
img = draw_ellipse(x{i},y{i}, a{i},b{i}, angle{i}, color{i}, img);
end
imshow(img)
I'm a bit unsure of what you want. You want to show several ellipse in one image, like plotting several graphs with hold on?
There is no equivalent command for images, but a simple solution is to add the ellipses into one image and show that one:
several_ellipse = ellipse1 + ellipse2 + ellipse3;
imshow(several_ellipse)
Presumably you want to pass ret as the final input to the next call to draw_ellipse.

Resources