Clear Filters
Clear Filters

How count crack pixel for image process and compare it with original

3 views (last 30 days)
How can I find the ratio of no. of crack pixels in the processed image to the no. of crack pixels in ideal image as attached
  1 Comment
DGM
DGM on 11 Jul 2023
Don't save binarized images as JPG. Using JPG will damage the image and usually result in a larger file size for no benefit.
If your image is binarized (not the image in the JPG), use nnz() to count nonzero pixels. If you're reading the damaged image back from the JPG, you'll have to do a bunch of junk to try to salvage it before you can count anything.

Sign in to comment.

Answers (1)

Parth Saraf
Parth Saraf on 11 Jul 2023
Hi,
You can try using this code:
processedImage = imread('processed_image.png');
crackPixelsProcessed = sum(processedImage(:));
originalImage = imread('original_image.png');
crackPixelsOriginal = sum(originalImage(:));
ratio = crackPixelsProcessed / crackPixelsOriginal;
Hope this helps!!
  2 Comments
yasmin ismail
yasmin ismail on 11 Jul 2023
@Parth Saraf does the crack pixel in original image count whole pixels of the image width*length?
and crack pixel of the processed image only the white area of the image which is indication of crack or also the total number of pixels width*length? I want this ratio to measure the true positive fraction to evaluate my model which is used for crack detection , so what I said is correct or not?
DGM
DGM on 11 Jul 2023
Edited: DGM on 11 Jul 2023
This code will obviously just give you a meaningless number.
% the original image
original = uint8(repmat(0:255,[256,1]));
imshow(original)
% a logical mask selecting the dark half
mask = original < 128;
imshow(~mask)
% you're adding up ALL values without concern for _which_ values are relevant
% you're also ignoring the fact that the ROI is defined by DARK regions
% also, data scale differs by a factor of 256 - regardless of area
sumorig = sum(original(:))
sumorig = 8355840
% the scale of this image is [0 1]; the other is [0 255]
summask = sum(mask(:))
summask = 32768
% you're dividing a measure of area by a sum which is both irrelevant and mis-scaled
% the result is a meaningless number
garbagenumber = summask/sumorig
garbagenumber = 0.0039
If you want a reference mask to compare against, you need to create that reference mask. You can't just use the original photo.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!