仅对图像的特定部分应用霍夫变换-MATLAB [英] Apply Hough Transform to only certain section of an image - MATLAB
问题描述
我已经使用MATLAB为此处提到的一组单词创建了边界框
I have created bounding boxes for a set of words as mentioned here using MATLAB
绘制整个单词的边界框,而不是图像中的每个字符的边界框-MATLAB
现在,我想将每个边界框指定的区域的内容复制到另一个矩阵中,以进行进一步的计算.
Now, I would like to copy the contents of the region specified by each bounding box into another matrix for further computation.
给定边界框的坐标,我该怎么办?
How can I do that given the coordinates of the bounding box?
编辑
在使用@beaker提到的方法时发现了一个问题
Discovered a problem while using the method mentioned by @beaker
这是原始代码,其中binary
是二进制格式的图像文件.
This is the original code where binary
is the image file in binary format.
Ibox(:,cnt)
给我绘制的每个矩形的坐标.
Ibox(:,cnt)
gives me the coordinates of each rectangle drawn.
imshow(normal);
boxes = zeros(0,1);
Sp = 10.5;
binclosed = imclose(binary, strel('rectangle',[4 ceil(Sp/2)]));
[Ilabel,num] = bwlabel(binclosed);
Iprops = regionprops(Ilabel, 'BoundingBox');
Ibox = reshape([Iprops.BoundingBox],[4 num]);
for cnt=1:num
rectangle('position',Ibox(:,cnt),'edgecolor','r');
X = (Ibox(:,cnt))';
X1 = X(1);
Y1 = X(2);
W = X(3);
H = X(4);
X2 = X1 + W;
Y2 = Y1 - H;
coords = [X1,Y1,X2,Y2];
boxes = [boxes;coords];
end
现在boxes
正在存储每个区域的坐标.
要提取区域,我正在做
Now boxes
is storing each regions coordinates.
To extract the region, I was doing
for c=1:num
subimage = binary(boxes(c,1:2),boxes(c,3:4));
imshow(subimage);
end
这给了我原始图像的随机剪裁.
which gave me random clippings of the original image.
当我检查boxes
中的坐标值时,发现它们处于浮动状态.
When I checked the value of coordinates in boxes
I found that they were in float.
如果我的图像是二进制格式,那怎么办?
How is that happening if my image is in binary format?
推荐答案
相对于K.I.S.S.解决方案.
Relatively K.I.S.S. solution.
boxes = boxes - 0.5;
解决了浮点问题.
其余代码如下:
for c=1:num
ir=1;
for xcoord = boxes(c,1):boxes(c,2)
ic=1;
for ycoord = boxes(c,3):boxes(c,4)
z(ir,ic) = binary(ycoord, xcoord);
ic=ic+1;
end
ir=ir+1;
end
if(size(z) < 100)
break;
else
str = sprintf(saveFile,c);
imwrite(z, [folder '\cropped\' str],'jpg');
end
clear z;
end
在这里,我只是将裁剪后的图像保存到文件夹中,以供以后使用imwrite
使用.
Here I am just saving the cropped images to a folder for later use using imwrite
.
这篇关于仅对图像的特定部分应用霍夫变换-MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!