Search code examples
imagematlabfor-looptext-segmentation

for loop causing bad text segmentation in matlab


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


Solution

  • 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;
    

    enter image description here