How count crack pixel for image process and compare it with original
2 views (last 30 days)
Show older comments
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
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.
Answers (1)
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
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(:))
% the scale of this image is [0 1]; the other is [0 255]
summask = sum(mask(:))
% 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
If you want a reference mask to compare against, you need to create that reference mask. You can't just use the original photo.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!