How to store values from a for loop in an array/matrix?

1 回表示 (過去 30 日間)
JOEY HOESER
JOEY HOESER 2021 年 3 月 27 日
コメント済み: JOEY HOESER 2021 年 3 月 27 日
I have this given 10x10 pixel image, and I would like to create a function that returns the column and row position that has the most red pixels.This is what I have so far, I am stuck on how to correctly store all the values and not just return the ending column and row. In addition if two columns or rows have the same amount it should return the lowest index value. Thanks for the help.
img = 'Image16.tif';
myImg = imread(img);
mySize = size(myImg);
redArr = myImg(:, :, 1);
greenArr = myImg(:, :, 2);
blueArr = myImg(:, :, 3);
isRed = (redArr == 255) & (greenArr == 0) & (blueArr == 0);
for i = 1:mySize(1)
rowCount = (sum(isRed(i,:)));
for j = 1:mySize(2)
colCount = (sum(isRed(:,i)));
end
end
  2 件のコメント
dpb
dpb 2021 年 3 月 27 日
'... given 10x10 pixel image,... returns the column and row position that has the most red pixels."
There is only one color value at each pixel? How can there be a maximum position location?
JOEY HOESER
JOEY HOESER 2021 年 3 月 27 日
That's correct. The function wants the logical 1 or 0 maximum sum of the rows and columns of the image.

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

採用された回答

Jan
Jan 2021 年 3 月 27 日
img = randi([0,1], 10, 10, 3) * 255;
imgIsRead = (img(:, :, 3) == 255);
[v1, index1] = max(sum(imgIsRed, 1))
[v2, index2] = max(sum(imgIsRed, 2))
  1 件のコメント
JOEY HOESER
JOEY HOESER 2021 年 3 月 27 日
Thanks for the help, it worked for me!

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by