How to draw a inclined line which can separate image in percent of white pixels

7 ビュー (過去 30 日間)
I want draw a line which seperate the image white and black pixel areas as shown in the picture. I want to keep first point fixed and move the second point in X direction so that I can adjust the percent of white pixels on either sides. I used attahced code with polyfix function (to fix the point 1) to draw line but the problem with this code is that "it's not seperating picture based on percentage that mentioned in the picture". Is there any possibility to include these percentages in the code. Thanks in advance

採用された回答

Image Analyst
Image Analyst 2022 年 1 月 17 日
OK, since you were not supplying the binary image, I made one from the picture you posted. Try this:
% Initialization Steps.
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 = 18;
mask = imread('spray.png') > 128;
imshow(mask);
hold on;
axis('on', 'image')
% Define where you want the starting column to be:
% at column %1, or the last column on row 1 that is white.
startingColumn = 1;
startingColumn = find(mask(1, :), 1, 'last')
% Get the number of white pixels in the image
numWhitePixelsEntireImage = nnz(mask)
[rows, columns] = size(mask);
% Run along the bottom row getting a quadrilateral mask.
for col = columns : -1 : 1
fprintf('Col = %d. \t', col)
hLine = line([startingColumn, col], [1, rows], 'Color', 'r', 'LineWidth', 2);
% Make a mask
x = [startingColumn, columns, columns, col];
y = [1, 1, rows, rows];
hPlot = plot(x, y, 'y-', 'LineWidth', 4);
thisMask = poly2mask(x, y, rows, columns);
% And it with the image
thisMask = mask & thisMask;
% Count the number of pixels in the upper right quadrilateral.
% We're looking for the number in that quadrilateral to be 5% of the total.
numWhite = nnz(thisMask);
pctWhite = 100 * numWhite / numWhitePixelsEntireImage;
fprintf(' Number of white pixels = %d pixels = %.3f %% of %d.\n', numWhite, pctWhite, numWhitePixelsEntireImage)
if pctWhite > 5.0
% Now we have 5% in the upper right quadrilateral so we can quit.
% Go back a column to where it was less than the threshold.
col = col + 1;
delete(hLine);
break;
end
% Delete old graphics.
delete(hPlot);
delete(hLine);
end
delete(hLine);
line([startingColumn, col], [1, rows], 'Color', 'r', 'LineWidth', 2);
  3 件のコメント
Image Analyst
Image Analyst 2022 年 1 月 18 日
OK, but starting with your binary image, which is where my code starts, does my code work with your binary image?
Rohit Thokala
Rohit Thokala 2022 年 1 月 18 日
Yes sir, it's working even on the binary image. Thank you for your help

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 1 月 17 日
編集済み: Image Analyst 2022 年 1 月 17 日
That image is no good. You attached a screenshot with a huge white frame around it instead of the actual image.
[EDIT] OK after your edit, you attached the original image, but not the background image "Water_T_140_t_inj_15_P_05_M30001.tif" so there is no way we can run your code to get the actual binary image. Please attach it.
So I'll tell you how I'd do it
% Get the number of white pixels in the image
numWhitePixelsEntireImage = nnz(mask);
[rows, columns] = size(mask);
% Run along the bottom row getting a quadrilateral mask.
for col = columns : -1 : 1
% Make a mask
x = [1, columns, columns, col];
y = [1, 1, rows, rows];
thisMask = poly2mask(x, y, rows, columns);
% And it with the image
thisMask = mask & thisMask;
% Count the number of pixels in the upper right quadrilateral.
% We're looking for the number in that quadrilateral to be 5% of the total.
numWhite = nnz(thisMask);
if numWhite / numWhitePixelsEntireImage > numWhitePixelsEntireImage * 0.05
% Now we have 5% in the upper right quadrilateral so we can quit.
% Go back a column to where it was less than the threshold.
col = col + 1;
break;
end
end
Now you know the column number where it broke so you're done. You can draw a line if you want at the final dividing line.
line([1, col], [1, rows], 'Color', 'r', 'LineWidth', 2);
  2 件のコメント
Image Analyst
Image Analyst 2022 年 1 月 17 日
See my other answer rather than this one.

サインインしてコメントする。

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by