How can I analyze and isolate certain region of 3D plot and create an condition for passing/failing parts?
4 次查看(过去 30 天)
显示 更早的评论
im1=imread('knifetest.jpg');
zr=im1(:,:,1); %get the red intensity array
zg=im1(:,:,2); %get the green intensity array
zb=im1(:,:,3); %get the blue intensity array
[r,c]=size(zg); %image dimension
subplot(2, 1, 1);
imshow(im1);
title('RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Set up figure properties:
subplot(2, 1, 2);
grid on;
hold on;
surf(1:c,1:r,zg,'EdgeColor','none'); %surface plot of green intensity
zlabel('Green Intensity')
%% How do I isolate a certain region in the surface to analyze and create a pass/fail condition. Example: if the green intensity in the middle is larger than X or the area of green is present inside a certain region is larger than X value, then sample fails
Also, can I plot a 2d image and its surf plot on the same plot using subplot?
Code credit to William Rose
2 个评论
William Rose
2022-7-25
@omar soufan, I made a comment back on your original post, which answers some of these quesitons.
回答(2 个)
prabhat kumar sharma
2023-10-13
Hi OmartheEngineer,
I understand that you are facing issue with isolating a region based on condition.
I found out a way that this can be done using the threshold value of area and intensity in the green channel of image. You can see below code for reference.
% Define the threshold values for pass/fail condition
thresholdIntensity = X; % Replace X with the desired threshold intensity value
thresholdArea = Y; % Replace Y with the desired threshold area value
% Create a binary mask based on the green intensity threshold
passMask = zg > thresholdIntensity;
% Calculate the area of the pass region
passArea = sum(passMask(:));
% Check the pass/fail condition
if passArea > thresholdArea
disp('Sample fails');
else
disp('Sample passes');
end
And plotting a 2d image and it’s surf plot on the same plot is already done in the code provided.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1509914/image.png)
Regards,
Prabhat Sharma
0 个评论
Shubham
2023-10-13
I understand that you want to isolate a region in the image and then analyse it.
The “imread” function returns a matrix consisting of indices and colour values for the pixels. Refer to the following example from the documentation:
[X,cmap] = imread('corn.tif');
You can isolate the region of your interest from the matrix values. For example, refer to the following code snippet:
grayImage = imread('pout.tif');
imshow(grayImage);
figure;
sliceOfImage = grayImage(:,1:50);
imshow(sliceOfImage);
The above code snippet crops the image such that only first 50 columns of the matrix are used to show the image. Here is the output produced by the above code snippet:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1510014/image.png)
You can also manually select the region of interest using functions such as “imrect” and “imcrop”. Refer to this previous MATLAB answer for extracting a region from the image:
Once you obtain the region of interest you can easily analyse colour intensities by comparing with a threshold value.
Also, for plotting a 2D image and its surf plot on the same plot using subplot can be achieved. Here is an example code snippet:
% Load the image
im1 = imread('peppers.png');
% Create a new figure
figure;
% Plot the 2D image
subplot(1, 2, 1);
imshow(im1);
title('RGB Image');
% Plot the surface plot
subplot(1, 2, 2);
surf(1:c, 1:r, zg, 'EdgeColor', 'none');
title('Surface Plot of Green Intensity');
xlabel('Column');
ylabel('Row');
zlabel('Green Intensity');
It produces the following output:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1510019/image.png)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Read, Write, and Modify Image 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!