How to get the pixel values of specific object in an image?

27 views (last 30 days)
Hi everyone! I would like to obtain the pixel values of a very thin and little streak (the one circled in red in this figure) . I have some problems because this object is very far. I have previously used this working code on a much larger and closer stripe, but for this strip, it doesn't seem to work. Can someone help me?
clc; clear all; close all;
A=rgb2gray(imread('Img_0222.jpg'));
B=bwareafilt(imbinarize(A-medfilt2(A,[5,5])),1);
pixelValues=A(B)
figure
imshow(B)

Accepted Answer

Image Analyst
Image Analyst on 8 Nov 2021
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
fileName = 'white spots.jpg';
grayImage = imread(fileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Extract the blue channel (so the magenta lines will be white).
grayImage = grayImage(:, :, 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
drawnow;
n = 47;
kernel = ones(n)/n^2;
blurredImage = imfilter(grayImage, kernel);
subplot(2, 2, 2);
imshow(blurredImage, []);
% Get a profile
horizontalProfile = improfile(blurredImage, [1, columns], [rows/2, rows/2]);
verticalProfile = improfile(blurredImage, [columns/2, columns/2], [1, rows]);
horizontalProfile = movmean(horizontalProfile, 199);
verticalProfile = movmean(verticalProfile, 199);
subplot(2, 2, 3);
cla
plot(horizontalProfile, 'b-', 'LineWidth', 2)
hold on;
plot(verticalProfile, 'r-', 'LineWidth', 2)
grid on;
backgroundImage = zeros(rows, columns);
for col = 1 : columns
for row = 1 : rows
backgroundImage(row, col) = sqrt(horizontalProfile(col)^2 + verticalProfile(row)^2);
end
end
backgroundImage = rescale(backgroundImage, 0, 1);
subplot(2, 2, 2);
imshow(backgroundImage, []);
impixelinfo;
title('Background Image', 'FontSize', fontSize, 'Interpreter', 'None');
grayImage = uint8(double(grayImage) ./ backgroundImage);
%--------------------------------------------------------------------------------------------------------
subplot(2, 2, 4);
imshow(grayImage, []);
title('Background Corrected Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% grayImage = grayImage(1800:2200, 1800:2200);
figure;
mask = grayImage == 255;
mask = bwareafilt(mask, [60, inf]);
imshow(mask)
% Get long skinny blobs only.
props = regionprops(mask, 'Area', 'MajorAxisLength', 'MinorAxisLength');
% allAreas = sort([props.Area], 'descend')
aspectRatios = [props.MajorAxisLength] ./ [props.MinorAxisLength];
keepers = find(aspectRatios > 5);
labeledImage = bwlabel(mask);
mask = ismember(labeledImage, keepers);
% Re-analyze with new mask.
props = regionprops(mask, 'Area', 'MajorAxisLength', 'MinorAxisLength');
imshow(mask)
allAreas = sort([props.Area], 'descend')
aspectRatios = [props.MajorAxisLength] ./ [props.MinorAxisLength]
title('Final Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
  6 Comments
Gargolla9
Gargolla9 on 8 Nov 2021
I think that 255 Is acceltable, but I have to apply your code to other 30 frames of the same object (these frames are very similar to the One that I have posted). So I would like to understand in which part of the code you can extract the exact information about pixel values (At the end I have to plot pixel values of the streak in each frame vs UTC Gregorian)
Image Analyst
Image Analyst on 9 Nov 2021
Edited: Image Analyst on 9 Nov 2021
Once you have the final mask you can get the pixel values like this:
pixelvalues = grayImage(mask);
Those will be the background corrected image values. If you want the original values (darkening as it goes to the sides) then you'll have to save the original image or not overwrite it.
originalImage = grayImage; % Save a copy of the original image, before background correction.
grayImage = uint8(double(grayImage) ./ backgroundImage);
% Then more code to get the mask, then:
% Get original pixel values:
pixelvalues = originalImage(mask);
% Get background corrected pixel values.
pixelvalues = grayImage(mask);

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 6 Nov 2021
I'm not seeing a "stripe" in the middle of the image, just white spots. Can you outline it in red? Maybe you can sum the image horizontally and look at the profile.
verticalProfile = sum(A, 2);
plot(verticalProfile, 'b-', 'LineWidth', 2);
grid on;
  5 Comments
Image Analyst
Image Analyst on 6 Nov 2021
I'm watching football now. No time for several hours. Really, it's not hard, try it yourself after you've gone through my Image Segmentation Tutorial here:
Image Analyst
Image Analyst on 7 Nov 2021
OK, so I'm sure by now you've called
mask = imbinarize(grayImage, 'adaptive');
props = regionprops(mask, 'MajorAxisLength', 'MinorAxisLength');
aspectRatios = [props.MajorAxisLength] ./ [props.MinorAxisLength];
keepers = aspectRatios > 2;
labeledImage = bwlabel(mask);
mask = ismember(labeledImage, keepers)
props = regionprops(mask, 'MajorAxisLength', 'MinorAxisLength');
So, how did it go?

Sign in to comment.


yanqi liu
yanqi liu on 8 Nov 2021
clc; clear all; close all;
A=rgb2gray(imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/791924/image.jpeg'));
A2 = imresize(A,0.2,'bilinear');
B2 = imbinarize(A2,'adaptive','ForegroundPolarity','dark','Sensitivity',0.7);
B3 = imopen(B2, strel('line', 7, 135));
[r,c] = find(B3);
B2 = bwselect(B2, c, r);
[L,num] = bwlabel(B2);
stats = regionprops(L);
for i = 1 : num
recti = stats(i).BoundingBox;
areai = stats(i).Area;
if recti(3)/size(B2,2) > 0.05 || recti(4)/size(B2,1) > 0.05 || recti(4)/size(B2,1) < 0.01 || areai > 50
B2(L==i) = 0;
continue;
end
if recti(1)/size(B2,2) > 0.2 && recti(2)/size(B2,1) > 0.2 && (recti(1)+recti(3))/size(B2,2) < 0.8 && (recti(2)+recti(4))/size(B2,1) < 0.8
else
B2(L==i) = 0;
continue;
end
end
B2 = imdilate(bwperim(imdilate(B2, strel('disk', 9))), strel('disk', 2));
A31 = A2; A32 = A2; A33 = A2;
A31(B2) = 255;A32(B2) = 0;A33(B2) = 0;
A3 = cat(3,A31,A32, A33);
figure; imshow(A3);

Community Treasure Hunt

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

Start Hunting!