Detect the vertical and horizontal lines and than crop the area that is hand written
古いコメントを表示
Hello, I have a complete data set of images like this around 1000+, what i want to do is to detect the vertical and horizontal lines and than crop the area that is hand written. After cropping each hand written character should be saved as an individual image. Any help will be appreciated. Thanks

採用された回答
その他の回答 (1 件)
You can use bwconncomp and regionprops to find the centroids of each whitespace box. The centroids can be used to generate subimages, "cropping" the target image. Here is an example:
Read image to file and convert it to a black and white image using imbinarize. Note that the threshold being used is 0.8. The threshold will depend on the image. You can use the greythresh function to dynamically generate the threshold, but the threshold may still have to be tuned.
I=imread('013.jpg'); %insert image name in place of ‘013.jpg’
greyIm=rgb2gray(I);
bwIm=imbinarize(greyIm,0.8);
Find the connected regions in the array using bwconncomp:
numPixels=cellfun(@numel,CC.PixelIdxList); %For each connected component, calculate the number of pixels.
boxIndices=find(numPixels>22500);
Calculate the centroids of all connected regions. Refer to the documentation of regionprops for more detail:
S=regionprops(CC,'Centroid');
centroids=cat(1,S.Centroid);
Verify that all whitespace box centroids have been found:
figure;
imshow(bwIm);
hold on
plot(boxCentroids(:,1),boxCentroids(:,2),'b*')
hold off
Create subimages from the whitespace box centroids using matrix indexing on the black and white image. In this case, I created a 200x200 pixel subimage around the second box centroid. Note that the centroids are floating point numbers, and have to be rounded using ceil to integers. This can be automated with a loop:
%obtaining one box:
Xrange=ceil(boxCentroids(2,1))-100:ceil(boxCentroids(2,1))+100; %each box is approximately 200x200 pixels.
Yrange=ceil(boxCentroids(2,2))-100:ceil(boxCentroids(2,2))+100;
boxIm=bwIm(Yrange,Xrange);
figure;
imshow(boxIm)
One of the box centroids (using the sample image the first one) is actually the centroid of the grid, and thus will produce a noncentered image. All other centroids should be the centroid of the whitespace boxes. Imwrite can be used to save the image.
5 件のコメント
Raja Bilal Rsb
2017 年 7 月 18 日
Kevin Xia
2017 年 7 月 19 日
The code will work for single files, and will not work for a directory path. However, it is possible to place the entire code body in a for loop so that it can. You can pull image names from the directory path using:
imageFiles=dir('*.jpg') %or any other desired image extension
imageFileNames={imageFiles.name};
See the documentation of dir for more details. You'll have to be in the directory with the image files in order for the above to work. With regards to resizing the subimages, you can either change the indexing from 100 to 14, as follows:
Xrange=ceil(boxCentroids(2,1))-14:ceil(boxCentroids(2,1))+14; %each box is approximately 200x200 pixels.
Though this might cut off the letter. Alternatively, you can use imresize:
newIm=imresize(boxIm,scale)
See the documentation on imresize for more details
Raja Bilal Rsb
2017 年 7 月 19 日
編集済み: Raja Bilal Rsb
2017 年 7 月 19 日
Image Analyst
2017 年 7 月 19 日
編集済み: Image Analyst
2017 年 7 月 19 日
After I fixed the first few problems, there were more. And the more I fixed it, the more it started to approach my code, for example you'd need to call imclearborder(), imfill(), fix the output filename, get the cropped image size correct, etc. So might as well just use my code, which already works. There is a reason my code is usually longer than others - it's flexible, general, robust, and extensively commented.
Raja Bilal Rsb
2017 年 7 月 20 日
カテゴリ
ヘルプ センター および File Exchange で Vehicle Network Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!








