![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/714002/image.png)
Crop image in diagonal direction
4 次查看(过去 30 天)
显示 更早的评论
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/713972/image.jpeg)
The result of imcrop on this is as follows(i am interested in getting m,a,j ,o ri and ty)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/713977/image.jpeg)
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.
0 个评论
采纳的回答
DGM
2021-8-17
编辑:DGM
2021-8-17
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/714002/image.png)
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 个评论
DGM
2021-8-17
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!