MATLAB Answers

## How can I count handwritten spots from an image??

Asked by Vic

### Vic (view profile)

on 27 Oct 2012

I took an A4 paper which has blank small rectangles on it and filled some of them with my pen. I then scanned the document and saved the image as .tiff. I converted the image in grayscale and in binary form because I thought it might be useful.

My Question is: Is there a way to count how many small rectangles I filled?

Thanks!!

## 2 Answers

### Walter Roberson (view profile)

Answer by Walter Roberson

### Walter Roberson (view profile)

on 27 Oct 2012
Edited by Walter Roberson

### Walter Roberson (view profile)

on 27 Oct 2012

Threshold the image. Label it. The number of labels is the number of rectangles.

If necessary, you can use regionprops() on the labeled image and change the Area measure in order to eliminate stray marks.

Vic

### Vic (view profile)

on 27 Oct 2012

wouldn't this count the all rectangles in the image? I want to count how many rectangles are filled(handwritten filled and then scanned)

Thanx

Walter Roberson

### Walter Roberson (view profile)

on 27 Oct 2012

The regionprops Area will be different between filled rectangles and ones that are not filled.

### Image Analyst (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

on 28 Oct 2012

Like Walter said, threshold and label:

```binaryImage = grayImage < 128; % or whatever intensity works.
[labeledImage numberOfRectangles] = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'Area');
allAreas = [measurements.Area];
```

You might find my image segmentation tutorial useful. It does conversion of grayscale into binary, then labeling and calling regionprops() to get the area. The Euler number (a count related to how many holes are in the region) would also be different for the filled vs. hollow rectangles.

Then it uses ismember to pick out the regions with area above a certain number of pixels (just what you need). Check it out for more info.

Vic

### Vic (view profile)

on 29 Oct 2012

I have this code so far :

```ImageFile='C:\Users\Victoras\Desktop\CONVERTION\Shape Recognation\answer.tiff';
% Step 1: Read image Read in
RGB = imread(ImageFile);
figure,
imshow(RGB),
title('Original Image');
```
```% Step 2: Convert image from rgb to gray
GRAY = rgb2gray(RGB);
figure,
imshow(GRAY),
title('Gray Image');
```
```% Step 3: Threshold the image Convert the image to black and white in order
% to prepare for boundary tracing using bwboundaries.
threshold = graythresh(GRAY);
BW = im2bw(GRAY, threshold);
figure,
imshow(BW),
title('Binary Image');
```
```% Step 4: Invert the Binary Image
BW = ~ BW;
figure,
imshow(BW),
title('Inverted Binary Image');
```

But I don't understand how to label all the rectangles what are handwritten filled. Can you give me more guidance because I am kind of new on this.

Thanx a lot though, very helpful staff

Walter Roberson

### Walter Roberson (view profile)

on 29 Oct 2012
```blobs = regionprops(BW,'Area');
```

now examine [blobs.Area] and notice there will be differences between the filled areas and the unfilled.

(Note: the people who answer questions here are volunteers, not staff.)

Image Analyst

### Image Analyst (view profile)

on 29 Oct 2012

I can tell you never tried out my tutorial because it goes over that. It finds and labels the nickels and dimes.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

New to MATLAB?