Open image, read block of pixels, average the intensity of the block, display average intensity matrix

조회 수: 1 (최근 30일)
How would one open an image, block image in say, 20x20 pixel blocks and then get the average of each 20x20 pixel block's intensity? Is the conversion of RGB to Grayscale required, or just good practice? I then would like to generate a matrix with those averages. Still learning MatLab so any help would be greatly appreciated.

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 6월 13일
편집: KALYAN ACHARJYA 2019년 6월 13일
Considering Gray Image, if not convert to grayscale then perform
im=imread('Image.jpg');
%.................^^ Change as per image name
[rows colm]=size(im);
block1=im(1:20,1:20);
%..here ^^ you can choose the block acccordingly
avg=mean(block1(:));
disp(avg);
  댓글 수: 6
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 6월 13일
편집: KALYAN ACHARJYA 2019년 6월 13일
%This code is just for undestand how to move the window (20x20) over image
% Not recomended for multiple use of for loops, look other approaches of block operation
im=imread('Image.jpg');
%.................^^ Change as per image name
[rows colm]=size(im);
k=1;
for i=1:20:rows-20
for j=1:20:colm-20
block(k)=im(i:i+19,j:19);
current_block=block(k);
avg=mean(current_block(:));
fprintf('The Block Number %d and Avg is: %f',k,avg);
k=k+1;
end
end
Try with 10x10 matrices as an example, you will get the idea.
Don Simon
Don Simon 2019년 6월 13일
Thank you. I am getting a column average for A1-A20, then column 2 average is B1-B20, etc. instead of the full average from the A1-T20 block of 20 cells. I'll keep at it.

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

카테고리

Help CenterFile Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by