How to divide an image into 8 equal sectors?
조회 수: 3 (최근 30일)
이전 댓글 표시
I have an image. I need to divide the image into 8 equal sectors as shown below.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/163158/image.jpeg)
I have used the following code:
clear all;
close all;
im=imread('apple.jpg');
r=100;
out1 = ones(max(size(im,1),r*2)+2,max(size(im,2),r*2)+2,3).*255;
xoffset = floor((size(out1,2)-size(im,2))/2);
yoffset = floor((size(out1,1)-size(im,1))/2);
out1(yoffset:yoffset+size(im,1)-1,xoffset:xoffset+size(im,2)-1,:) = im(:,:,:);
im = out1;
cy = floor(size(im,1)/2);
cx = floor(size(im,2)/2);
figure;
imshow(uint8(im));
hold on
pos = [cx-r+1 cy-r+1 r*2 r*2];
rectangle('Position',pos,'Curvature',[1 1]);
x1 = [-r, 0, -r*cosd(45), -r*cosd(45); r, 0, r*cosd(45), r*cosd(45)]+cx+1;
y1 = [0, -r, -r*sind(45), r*sind(45); 0, r, r*sind(45), -r*sind(45)]+cy+1;
plot(x1,y1);
hold off
figure;
for i = 0:45:315
t = linspace(-i,-i-45,128);
x = [cx, cx+r*cosd(t), cx];
y = [cy, cy+r*sind(t), cy];
bw = poly2mask( x, y, size(im,1),size(im,2));
bw = repmat(bw,1,1,3);
out = ones(size(im,1),size(im,2),size(im,3)).*155;
out(bw) = im(bw);
subplot(2,4,(i/45)+1); imshow(uint8(out));
end;
I need to do this for a dataset of over 100 images with different sizes. My problem is that, I have to manually assign the value for r for every image. How can I automatically assign the value for r? Please help. I also need to minimize the background (white parts) in the sectors as much as possible. How do I go about this? Please help. Thank you.
댓글 수: 0
채택된 답변
Image Analyst
2017년 4월 20일
Just mask the image with sector shapes. See the FAQ:
댓글 수: 3
Image Analyst
2017년 4월 24일
Find the apple mask by converting to hsv colorspace with rgb2hsv() then thresholding the S channel. The apple will have S values more than about 0.2. Now you have a mask and you can use it to blacken outside the apple, crop the image, or both.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!