How to divide an image into 8 equal sectors?

I have an image. I need to divide the image into 8 equal sectors as shown below.
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.

 채택된 답변

Image Analyst
Image Analyst 2017년 4월 20일

0 개 추천

댓글 수: 3

anastasia
anastasia 2017년 4월 21일
But even in those links, the radius has to be pre-specified. My question was how to automatically decide the radius value instead of pre-specifying every time for my images which are of different sizes
anastasia
anastasia 2017년 4월 24일
I am getting the background white pixels into my sectors. How do I eliminate those?
Is it possible to split ONLY the apple image into equal parts using the centroid and the major and minor axis?
I do not want the background pixels in my split parts, just the ROI pixels.
Please guide.
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개)

카테고리

도움말 센터File Exchange에서 Images에 대해 자세히 알아보기

질문:

2017년 4월 20일

댓글:

2017년 4월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by