How can I read all 0 values from all rows and columns?

조회 수: 2(최근 30일)
Pedro Marques
Pedro Marques 2020년 12월 17일
댓글: Image Analyst 2020년 12월 18일
Hi guys,
I'm trying to draw a centre point for all the rectangles from the picture below.
When I import the image and convert using the function "rgb2gray" I end up getting a 2D table.
The point here is to draw a line so I can follow a trajectory.
I tried to use some computer vision functions but it wasn't working as expected so I think the best approach will be getting the centre point and then draw a line connecting all the dots.
The zero values from my table represent the black lines from the image.
This is what I have.
screen=imread('screenshoot_S10+_screen_test.jpg') %importing image
gray_rows =rgb2gray(screen) %converting into a 2D table
gray= gray_rows' %converting previews rows into columns
l = find((gray_rows( ) == 0))
c= find( (gray()== 0))'
For some reasson my c var is not reading the rows of table "grey".

채택된 답변

Image Analyst
Image Analyst 2020년 12월 18일
Upload a PNG file. You can't really count on the image if it's a JPG image. The lines may be blurry due to lossy compression artifacts. If you just find the centroid of every square and rectangle (ones with no other ones niterior to them) then you're going to have centroids all over the place. How is it then supposed to know what trajectory you want?
In the meantime, this is what I have so far:
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 = 22;
folder = pwd;
baseFileName = 'rectangles.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
grayImage = imread(fullFileName);
% 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.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
% Display the image.
subplot(1, 2, 1);
axis('on', 'image');
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
% Get a mask that is the entire image.
mask = grayImage == 255;
% Get rid of small things
mask = bwareaopen(mask, 50);
% xline(threshold, 'LineWidth', 2, 'Color', 'r');
h3 = subplot(1, 2, 2);
imshow(mask, []);
axis('on', 'image');
title('Mask Image with Rect Centroids', 'FontSize', fontSize, 'Interpreter', 'None');
% Get the centroid of each blob.
props = regionprops(mask, grayImage, 'Centroid', 'Area');
allAreas = sort([props.Area])
xy = vertcat(props.Centroid)
numBlobs = length(props)
hold on
plot(xy(:, 1), xy(:, 2), 'r.', 'MarkerSize', 20);
  댓글 수: 4
Image Analyst
Image Analyst 2020년 12월 18일
I don't think you can determine the path if you don't also have timing information. If all you have is an image, those locations could have been made in millions of different ways. For example you could draw the outer box first then the X or you could draw 4 triangles. There is no way to know without timing info.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by