Crop image in diagonal direction

9 views (last 30 days)
Sparsh Garg
Sparsh Garg on 17 Aug 2021
Commented: DGM on 17 Aug 2021
Imcrop allows us to select a rectangular region that we can later on crop.
However,given an image as shown below,the result of imcrop is not always sufficient.I would like to know ,if there is a way to perform the cropping at an angle.
The result of imcrop on this is as follows(i am interested in getting m,a,j ,o ri and ty)
As we can see that some part of the o has been included in this.So that's why I am looking for a better cropping method.
Note i tried regionprops,but unfortunately all the images are subjected to a bw operation which is not what i want.
Nor do i want imfreehand as i wasn't able to get the correct results.

Accepted Answer

DGM
DGM on 17 Aug 2021
Edited: DGM on 17 Aug 2021
This isn't really a complete solution, but oh well.
Just because regionprops does binarization doesn't mean that the result needs to be binary. You can use the mask to select regions from the image. The masks don't even necessarily need to have hard edges. For example:
a=imread('majority.jpg');
c=~imbinarize(a);
cl = bwlabel(c);
S=regionprops(c,'boundingbox');
bb = vertcat(S.BoundingBox);
maxbb = max(bb(:,[4 3])); % minimal box geometry
maxbb = maxbb+5; % add some extra room
numchars = numel(S);
for j=1:numchars
tp = round(S(j).BoundingBox);
xrange = tp(1)+(-1:tp(3)+1);
yrange = tp(2)+(-1:tp(4)+1);
% start out with no masking
target = a(yrange,xrange);
% use a dilated mask
tm = double(imdilate(cl(yrange,xrange)==j,ones(3)));
% apply the mask
target = im2uint8(im2double(target).*tm + ones(size(tm)).*(1-tm));
% this pads with black to match sizes
bs = (maxbb-size(target))/2;
target = padarray(target,floor(bs),255,'pre');
target = padarray(target,ceil(bs),255,'post');
ax=subplot(3,3,j);
imshow(target);
end
Now of course, segmenting by region connectivity isn't going to solve the problem. There's no trivial way to break apart multiple letters made with the same stroke, and there's no way to associate the dots with their corresponding characters. How is that done? Fancier binary segmentation? Polygonal ROI's? I don't know, but using imcrop() isn't the answer.
  2 Comments
Sparsh Garg
Sparsh Garg on 17 Aug 2021
thanks any reason why the dot got broken into two different components.
As we can see in this image,the i was correctly associated with the '.'
So when you said fancy binary segmentations or polygonal ROIs,would you please let me know of some other methods.
BTW here is the RegionProps code that I was referring to,apparently after doing the im2bw there is a morphological opening which I am assuming would help in retainging the association between the dot and the I.
DGM
DGM on 17 Aug 2021
The first dot is missing because you have bwareaopen() set with a 30px threshold. The dot has an area of 14px.
There is no association with the dot and anything else. These are all just connected pixel regions. The T and Y are one object. The dot from the J is one object. Nothing here describes the relationship with the J and its lone dot.
The opening is simply used to remove small features (noise) from the background.
A polygonal ROI can be made by creating an images.roi.polygon object and setting its position property. It can then be used to create a mask for selection if desired. This doesn't really address the problem though, as you still need to programmatically discern where the letters are in order to know where to put the polygon vertices.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!