What would black be in an RGB triple
3 views (last 30 days)
Show older comments
I am creating code to analyse the colours of pixels in 2 images - one hand segmented and 1 neural network image. I would like to see if both pixels are black or not black to work out the accuracy, specificity and sensitivity. therefore I would like to know what the correct way of defining black is so i can add it as a define statement at the top.
%import the 2 files (hand segmented and NN segmented)
hand_seg = ['Z:\eee_biophotonics1\Shared\SPOT dataset (Training data)\' ...
'Testing Images\123 images on 123 network\hand segmented'];
dir_hand_seg = dir(hand_seg);
NN_seg = ['Z:\eee_biophotonics1\Shared\SPOT dataset (Training data)\' ...
'Testing Images\123 images on 123 network\nn segmented'];
dir_NN_seg = dir(NN_seg);
%set parameters
colour_hand; %black is not epidermis, grey is
colour_NN; %black is not epidermis, grey is
x; y; %dimensions of images and number of image in file
x_max = 400; y_max = 512; %sizes of images
FP = 0; FN = 0; TP = 0; TF = 0; %for confusion matrix
acc; spec; sens;
nfiles = length(dir_hand_seg);
%loop for image scanning
for i = 1 : nfiles
%opens hand seg images
file_hand = dir_hand_seg(nfiles).name;
full_dir_hand = fullfile(hand_seg,file_hand);
im_hand = imread(full_dir_hand, "tif");
%opens hand seg images
file_NN = dir_NN_seg(nfiles).name;
full_dir_NN = fullfile(NN_seg,file_hand);
im_NN = imread(full_dir_NN, "tif");
for x = 1:x_max
for y = 1:y_max
colour_hand = impixel(im_hand,x,y); %gets colour value of pixel
% of gold standard
colour_NN = impixel(im_NN,x,y); %gets colour value of pixel of
% NN
%adds values to the confusion matrix
if colour_hand == 1 && colour_NN == 1 %not epidermis for gold
%standard, not epidermis for NN (true negative)
TN = TN + 1;
elseif colour_hand == 1 && colour_NN ~= 1 %not epidermis for
%gold standard, epidermis for NN (false positive)
FP = FP + 1;
elseif colour_hand ~= 1 && colour_NN == 1 %epidermis for the
%gold standard, not epidermis for the NN (false negative)
FN = FN + 1;
elseif colour_hand ~= 0 && colour_NN ~= 0 % not epidermis for
%the gold standard, not epidermis for the NN (true negative)
TN = TN + 1;
end
end
end
fclose(im_hand); %closes image
fclose(im_NN); %closes image
end
%checks for accuracy, specificity and sensitivity
acc = (TP + TN)/(TP + TN + FP + FN);
spec = (TN)/(TN + FP);
sens = (TP)/(TP + FN);
3 Comments
John D'Errico
on 18 Dec 2022
No. You don't need to take one of the RGB values. Just use 0.
A gray scale image has only one channel. (Though you COULD have a 3 channel gay scale image.) But assuming your image has only one channel, then just use zero. Typically, such an image wil be scaled on the interval [0,1] or [0,255]. the latter scaling would mean it is stored as uint8. Regardless, zero is still black.
Answers (1)
DGM
on 18 Dec 2022
Moved: DGM
on 7 May 2023
Unless you are dealing with clean binarized or labeled images, or unless you have a particular reason to be so exact, what a person calls "the black part of an image" is often not exactly zero, but a range of values near zero. Not all uses of the word are so specific.
Consider the example of the following image.
inpict = imread('blobs.jpg');
imshow(inpict)
That looks pretty straightforward. There are five shades of gray on a black background. Let's select the background by assuming "black" is exactly zero.
blackregions = inpict == 0;
imshow(blackregions)
Instead, a reasonable selection can be obtained by selecting a range near zero.
blackregions = inpict <= 30;
imshow(blackregions)
Similarly, consider this image. What objects in this image are black? The man's jacket? The tripod legs?
inpict = imread('cameraman.tif');
imshow(inpict)
If "black" is interpreted as exactly zero, then not a single pixel in this image is "black".
min(inpict(:))
What's any of that mean in your context? I don't know.
1 Comment
DGM
on 7 May 2023
Edited: DGM
on 7 May 2023
I should point out that "black" isn't always some range of values near zero -- not even approximately. If your image is int16, "black" is -32768, not 0.
inpict = imread('cameraman.tif');
inpict = im2int16(inpict);
min(inpict(:)) % the darkest pixel isn't exactly black, but close
That said, you're probably not going to run across too many int16 images unless you're dealing with medical images, in which case, the image data (in my experience) often doesn't come close to spanning the available dynamic range. So again, one has to define whether "black" is the nominal minimum value implied by the class, or whether it's defined by the image extrema or object content.
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!