how can i remove white spaces around this image ?

 Accepted Answer

img = imread('whiteBG.jpg');
img = rgb2gray(img);
figure, imshow(img)
% count the pixels of each bin
[counts, bins] = imhist(img);
[M, I] = max(counts);
tempImg = img;
% we know that max pixels in image are white so change all pxiels in that
% bin and above to zero
tempImg(img >= bins(I)-1) = 0;
figure, imshow(tempImg)
% clear some traces or blobs having area less than 30 pixels ...
% (can change according to requirement)
BW = bwareaopen(tempImg,30);
figure, imshow(BW)

5 Comments

Thank you , I used imcrop() and the results not good as I need maybe because of the rotation problem ... I could not deskew the image correctly until now .have you any idea about that ???
I saw your other question on deskew, I tried something but it's not entirely correct.
Extension to the above code:
%%perform pca dilated white points
temp = imdilate(BW, strel('disk',20));
figure, imshow(temp)
[r, c] = find(temp);
coeff = pca([r,c]);
angle = atan2(coeff(1,1), coeff(1,2));
% rotate back
deSkew = imrotate(BW, angle / pi * 180,'nearest','crop');
figure, imshow(deSkew)
Wait for someone who knows better, If I figure it I will post my answer too
If you know the angle of rotation it is easy to get the deskew, maybe you can try different angles and try to get a clear deskewed image.
I found:
deSkew = imrotate(BW,6,'nearest','crop');
gives a decent output
Usually, the above code should deskew based on non-zero pixels but the information is not so continuous so I didn't get good rotation
i will try it thank you
the results good for this image but when I applied it on another image it didn't work will !

Sign in to comment.

More Answers (1)

It looks reasonably well cropped right now. Why do you need to chop off a few pixels more? What's the need? I think you can do what you need to do without cropping to the bounding box, so why bother?

5 Comments

I have to remove all around spaces for framing braille character for the recognition phase .
May you help with the deskewing of the image ?
You might be able to use a radon transform, like in the attached demo, but you'd have to adapt it to find the profile with the greatest contrast.
But I think maybe I'd try to use hough to detect lines. Then find the average angle of all the lines it finds. If that doesn't work you can find the bounding box of the dots with bwconvhull() and then try to identify the 4 sides and determine which are parallel or 90 degrees to each other. The minimum perimeter polygon algorithm may be useful here. On your bottom, because the line doesn't go all the way over, the top line will not be parallel to the bottom line of the convex hull.
I tried to use hough before i think I have a problem using it .
clc ; close all ; img = imread('ko.jpg'); img = rgb2gray(img); % count the pixels of each bin [counts, bins] = imhist(img); [M, I] = max(counts); tempImg = img; % we know that max pixels in image are white so change all pxiels in that % bin and above to zero tempImg(img >= bins(I)-1) = 0; figure(1), imshow(tempImg) % clear some traces or blobs having area less than 30 pixels ... % (can change according to requirement) BW = bwareaopen(tempImg,30); figure(2), imshow(BW);
%% rotate the bwf image by theta angle using hough transform se = strel('line',30,0); bb1 = imdilate(BW,se); bb1= bwmorph(bb1,'thin',Inf); [H,T,R] = hough(bb1,'RhoResolution',0.5,'ThetaResolution',0.5); P = houghpeaks(H,1); x = T(P(:,2)); y = R(P(:,1)); lines = houghlines(bb1,T,R,P,'FillGap',5,'MinLength',20);
what next ?
I already told you what to do next if that didn't work: "If that doesn't work you can......". See my above comment.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!