Can anyone help with this error? (I just want to see the contour of the bluish area at the bottom of the image)

close all
clear all
clc
I = imread('frame75.jpg');
imshow(I)
imcontour(I,3)
Error:
Error in imcontour>ParseInputs (line 110)
validateattributes(a,{'uint8','int16','uint16','double','logical','single'}, ...
Error in imcontour (line 40)
[x,y,a,extra_args] = ParseInputs(varargin{:});
Error in contour (line 8)
imcontour(I,3)

 Accepted Answer

Detect blue region properly
I0 = imread('frame75.jpg');
I1 = double(I0);
% colors from blue region
R = [80 50 0];
G = [160 110 70];
B = [180 120 70];
B1 = logical(I1(:,:,1)*0);
% detect regions and merge
for i = 1:length(R)
B0 = abs(I1(:,:,1)-R(i)) < 20 & ...
abs(I1(:,:,2)-G(i)) < 50 & ...
abs(I1(:,:,3)-B(i)) < 50;
B1 = B1 | B0;
end
% B1 = uint8( cat(3,B1,B1,B1) );
imshowpair(I0,B1)
% imshow(B1.*I0)

12 Comments

Hi thanks for the info.
how did you detect colors from the blue region ?
R = [80 50 0];
G = [160 110 70];
B = [180 120 70];
is it possible to contour the blue region only?
  • how did you detect colors from the blue region ?
I used impixelinfo
imshow(I0)
impixelinfo
  • is it possible to contour the blue region only?
Sure, try this
B2 = bwareaopen(B1,100); % remove small regions (100 pixels)
h = strel('disk',3,0); % dilation instrument
B3 = imdilate(B2,h); % dilate image
B4 = edge(B3); % detect edge
[ii,jj] = find(B4); % find coordinates of each edge pixel
imshow(I0)
hold on
plot(jj,ii,'.r')
hold off
zoom on
Thanks alot! you being really helpful: I want few explanation from you to understand few commands mentioned above!
1) B1 = logical(I1(:,:,1)*0); % is that command means reading the first row and column of red channel in I1 then in logical command 1 means true and 0 means false.....in this command it multiplied by 0 => what does that mean?
2) In loop shown below:
or i = 1:length(R)
B0 = abs(I1(:,:,1)-R(i)) < 20 & ... %absolute value of I1-R(i)
abs(I1(:,:,2)-G(i)) < 50 & ...
abs(I1(:,:,3)-B(i)) < 50;
B1 = B1 | B0;
% what does R(i) means and why we use <20, <50.....
what does B1 = B1 | B0 means
Apologise for asking so much, as I am new to image analysis and learning while I am processing the images through this platform! your help is much appreciated.
1) B1 = logical(I1(:,:,1)*0); % is that command means ...
No, it's just faster way to preallocate a matrix
[m,n,k] = size(I1); % I1 3d matrix
B1 = zeros(m,n); % B1 2d matrix (logical)
2) In loop shown below:
R(i) means red channel, G(i) means green channel, B(i) mean blue channel
You have different "blue" colors so i picked 3 of them:
R = [80 50 0];
G = [160 110 70];
B = [180 120 70];
Here is a simple example:
Imagine you have this image
i generated it using these command
[x,y,z] = peaks(50);
imagesc(z)
impixelinfo
Let's find colors in this region (value -6 approximately). How will you find them?
Let's find values
z1 = abs(z--6) < 2; % '2' is our tolerance
imshowpair(z1,z) % show images together
Green region is the region where values inbetween [-4 ... -8] ()
It's an example for gray image. Color image (RGB) has 3 matrices (Red, Green and Blue) so that is why i used tolerance for each channel.
Try to play with those values, see if you can detect better contour
3) what does B1 = B1 | B0 means
It means logical operand OR (simply '+'). As you can see we are searching for 3 colors (3 regions), so each for loop we are detecting different region. I want to merge them
Try to change this line
% for i = 1:length(R(i))
for i = 1
Here are results for each for loop
i = 1 i = 2 i = 3
Hi darova,
Thank you for the explanation. I am nearly finish with this project. If you can help me by creating the loop which will count the pixels in the interface that will be great. I have attached the image...
Explanation: I have captured the interface of the dye by using the code we discuss above now i want to see the pixels in frame 15, 16 , 17 and see the change
You want to calculate number of pixels inside the contour (area) or perimeter?
perimeter! as perimeter explaining the rough estimation of amount of pixel in the interface and if you see in the attached cropframe15.png everything is white background except the perimeter so I want to explain in the code that colour of pixels in the perimeter are x y z hence count the pixels in the perimeter and give me the total number of pixels....
Thanks
Number of pixels and perimeter are different things
The number of pixels but length of lines are different
Experiments with regionprops
I0 = false(100);
I0(30,30) = true;
I1 = imdilate(I0,strel('disk',10,0));
props = regionprops(I1,'perimeter');
props
2*pi*10
imshow(I1)
results
props =
Perimeter: 65.9411
ans =
62.8319
Hi Darova,
Sorry, I want the number of pixels in the contour (when you said perimeter I thought you mean number of pixels in boundary)..... I counted manually there are approximately 486 pixels. How would I can make a code of it to calculate them pixels.
I have attached you the image, which is basically the zoomed in image so one can count the pixels manually, but if I want to make a code for it how?
what you have explained above it is still very useful and cleared my thoughts regarding the pixels and parimeter calculations.
You the man! seriously..... It worked
I got the answer
ans = 431
just wondering when I counted manually answer was 486 and with binarizing 431... differrence in pixels are like 55. Do you know why there is that difference?
sum(~A1(:)) => means it is adding all the pixels in the image except white background
try to change threshold
th = graythresh(I0);
I1 = im2bw(I0,th);
imshow(I1)

Sign in to comment.

More Answers (1)

You need to specify a single channel image to imcontour(). For example, just provide the blue channel
imcontour(I(:,:,3), 3)
or convert image to grayscale
imcontour(rgb2gray(I), 3)

1 Comment

Hi,
Hamza please see the attached. I have changed to grey scale function to capture the contour of the injected dye flow but I am also getting the contours of other background is there a way to only capture the dye contour.
here is a code I used!
close all
clear all
clc
%Read the Image
I = imread('frame75.jpg');
I1 = imread('frame80.jpg');
warning('off', 'Images:initSize:adjustingMag');
%Convert to greyscale (contour command works on greyscale images)
img=rgb2gray(I);
img1=rgb2gray(I1);
%Subtraction of the frames
figure()
dif=img-img1;
warning('off', 'Images:initSize:adjustingMag');
imshow(dif)
hold on
% contour of the image
figure()
imcontour(dif,1);

Sign in to comment.

Asked:

on 27 Jun 2020

Commented:

on 3 Jul 2020

Community Treasure Hunt

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

Start Hunting!