Question

I want to count the number of edge pixels in a given image. I tried it by counting pixels of white color of the image we get by using Canny operator on the image.

I = rgb2gray(imread('replay1.jpg'));
bw = edge(I,'canny');
numberOfBins = 256;
[r, cl, x] = size(bw);
[pixelCount, grayLevels] = imhist(bw);
c = sum(pixelCount(pixelCount==255));
disp(c*100/(r*cl));

Questions:

1. But this somehow gives the same answer for all images, which suggests I am doing it wrong. How should I find number of edge pixels of an image in matlab?

2. Also can I use canny operator on YCbCr format of an image? I tried doing that but it gives me errors.

enter image description here

Was it helpful?

Solution

PART 1

Use this - count_edge_pixels = nnz(bw) This is a very efficient way to calculate true (1) values that are edge pixels in this case and thus, would give you count of edge/white pixels as calculated from edge.

PART 2

You can use edge on each of Y, Cb, Cr separately or just use Y for edge detection on the luminance part. Use this to get YCbCr from RGB images.

Let's suppose you would like to get edge information on the luminance map of the image, do something like this -

YCBCR = rgb2ycbcr(imread('replay1.jpg'));
luminance_map = YCBCR(:,:,1); 
bw = edge(luminance_map,'canny');

Hope this makes sense and works for you!

OTHER TIPS

pixelCount(2) will give you the number of edge pixels here. As @Divakar mentioned, nnz(bw) will also work as expected.

I = rgb2gray(imread('test.jpg'));
bw = edge(I,'canny');
numberOfBins = 256;
[r, cl, x] = size(bw);
[pixelCount, grayLevels] = imhist(bw);
count = pixelCount(2);  // <- here, or use "count = nnz(bw)"

To detect canny edges on YCbCr images, you can use edgecolor.m.

This is also efficient way to count the num of edge pixels.

count =length(find(BW(:)==1));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top