필터 지우기
필터 지우기

How to count the number of occurrences of pixel intensities in an image without using for loop?

조회 수: 8 (최근 30일)
I am to write a script for histogram equalisation and I need to work on each RGB plane separately. In the first step I count the number of occurrences of each intensity value in the range 0-255. As far as I know, using for loops makes MATLAB code super slow.
org_image = imread('image.jpg')
tot_pixel = size(org_image,1) * size(org_image,2)
R = org_image(:,:,1);
G = org_image(:,:,2);
B = org_image(:,:,3);
[R_val_ocurr,R_unique_val] = histcounts(R);
[G_val_ocurr,G_unique_val] = histcounts(G);
[B_val_ocurr,B_unique_val] = histcounts(B);
Now to have an array of size 256,with each index holding number of pixels corresponding to it what should my next step be? I'm trying to write down my logic :
for i = 0 to 255
if i is in R_unique_val
hist[i] = R_val_ocurr[i]
else
hist[i] = 0
Please tell me how to correctly and efficiently write this.
  댓글 수: 1
Stephen23
Stephen23 2018년 9월 7일
This is confusing, because the first part of your code gets a histogram, in fact your even write this "In the first step I count the number of occurrences of each intensity value in the range 0-255". So each of R_val_ocurr, etc. is a histogram. Then what else do you have to do?

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

채택된 답변

Stephen23
Stephen23 2018년 9월 7일
편집: Stephen23 2018년 9월 7일
im = imread('image.jpg');
Rhist = imhist(im(:,:,1));
Ghist = imhist(im(:,:,2));
Bhist = imhist(im(:,:,3));
Or using histcounts:
Rhist = histcounts(im(:,:,1),0:256);
Ghist = histcounts(im(:,:,2),0:256);
Bhist = histcounts(im(:,:,3),0:256);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by