필터 지우기
필터 지우기

How can I get the values of a circular region around a particular pixel?

조회 수: 8 (최근 30일)
How can I get the values of a circular region around a particular pixel?

채택된 답변

David Young
David Young 2014년 11월 3일
Assuming:
  • your image is grayscale and is called img;
  • the central pixel is at column x and row y;
  • the radius of the circle is r;
  • you do not require weighting at the rim of the circle - that is, each pixel is either entirely inside or entirely outside the circle;
then you can do something like this
[xgrid, ygrid] = meshgrid(1:size(img,2), 1:size(img,1));
mask = ((xgrid-x).^2 + (ygrid-y).^2) <= r.^2;
values = img(mask);
where values will have a column vector containing the values in the circular region.
  댓글 수: 3
Jennifer Bernier
Jennifer Bernier 2018년 11월 7일
I'm doing something similar but with 5000 unique circular ROIs on a single image, and I can't figure out how to scale this method without using a loop (which takes a long time). Does anyone have any suggestions?
Image Analyst
Image Analyst 2018년 11월 8일
5000 iterations of a for loop should take only a few microseconds Here is the timing on my system:
Elapsed time is 0.000010 seconds.
So, 10 microseconds. If yours is taking a lot longer, then there is something else that's taking the time, it's not the for loop itself.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 11월 3일
There's a FAQ on how to mask an image: http://matlab.wikia.com/wiki/FAQ#Image_Processing_Toolbox:
% Demo to have the user freehand draw an irregular shape over a gray scale image.
% Then it creates new images:
% (1) where the drawn region is all white inside the region and untouched outside the region,
% (2) where the drawn region is all black inside the region and untouched outside the region,
% (3) where the drawn region is untouched inside the region and all black outside the region.
% It also (4) calculates the mean intensity value of the image within that shape,
% (5) calculates the perimeter, centroid, and center of mass (weighted centroid), and
% (6) crops the drawn region to a new, smaller separate image.

Community Treasure Hunt

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

Start Hunting!

Translated by