1

Я работаю над созданием системы CBIR, где я должен сегментировать мой RGB изображение следующим образом:код для определения маски в MATLAB

Regions I want to divide my Image into

Я реализую код в MATLAB, но я я не могу создать для него подходящие маски. Я использовал imellipse, но для этого требуется ручка изображения, которая достигается с помощью imshow, но я не хочу показывать свое изображение. Мой код

img=imread('peppers.png'); 
h_im=imshow(img); %I want to get rid of imshow because I don't want to show the image 

[height, width, planes]=size(img); 
%(cX,cY) is image center 
cX=width/2; 
cY=(height)/2; 

%Here I define my ROI which is an ellipse that stretches to 75 percent of 
%height and width of the image 
e=imellipse(gca,[(1/2-3/8)*width, (1/2-3/8)*height,(3/4)*width,(3/4)*height]); 
mymask=createMask(e,h_im); 

%extending mask to three channels 
mymask=repmat(mymask,[1 1 3]); 
ROI=img; 
ROI(mymask==0)=0; 
figure, imshow(ROI); 

The output image

ответ

0

Немного взломать, но вы можете создать «скрытый» фигуру. Единственное различие заключается в том, что я добавил: figure('Visible', 'off') в начале вашего кода.

figure('Visible', 'off'); 

img=imread('peppers.png'); 
h_im = imshow(img); %I want to get rid of imshow because I don't want to show the image 

[height, width, planes]=size(img); 
%(cX,cY) is image center 
cX=width/2; 
cY=(height)/2; 

%Here I define my ROI which is an ellipse that stretches to 75 percent of 
%height and width of the image 
e=imellipse(gca,[(1/2-3/8)*width, (1/2-3/8)*height,(3/4)*width,(3/4)*height]); 
mymask=createMask(e,h_im); 

%extending mask to three channels 
mymask=repmat(mymask,[1 1 3]); 
ROI=img; 
ROI(mymask==0)=0; 
figure, imshow(ROI); 
+0

Это, кажется, работает для меня, Спасибо !, можете ли вы предложить мне некоторый подход к определению масок, чтобы получить регионы с номерами 1, 2, 3 и 4? – user164013

2

Вы можете создать эллипс маскировать себя, а не с помощью команды imellipse.

% Create a meshgrid the same size of the image in order to generate the mask 
[x y] = meshgrid(1:size(img, 1), 1:size(img, 2)); 

% Create the eclipse mask using the general form of an eclipse 
% This will be centered in the middle of the image 
% and have a height and width of 75% of th eimage 
A = (0.75/2)*size(img, 2); 
B = (0.75/2)*size(img, 1); 
mask = A^2*(x - floor(size(img, 1)/2)).^2 + B^2*(y - floor(size(img, 2)/2)).^2<=A^2*B^2; 

% Apply the eclipse mask 
masked_image = img.*repmat(mask, [1, 1, 3]); 
+0

Я получаю сообщение об ошибке, когда пытаюсь выполнить код, вы можете мне помочь? Ошибка: ??? Попытайтесь выполнить маску SCRIPT как функцию: C: \ Users \ dell \ Documents \ MATLAB \ mask.m Ошибка в ==> ellipsemask при 14 masked_image = img. * Repmat (маска, [1, 1, 3] ]); – user164013

+0

В вашем коде, где вы определили маску? – user164013

+0

Извините, исправлено. 'C' должен быть' mask'. – Sam

0

Я думаю, что этот код легко понять и легко настраиваются для решения произвольной части изображения (имеет тот же результат, как ваш код):

img = imread('peppers.png'); 

% define the size of your ellipse relatively to the image dimension 
factor = 0.75; 

hwidth = size(img, 2)/2.0; 
hheight = size(img, 1)/2.0; 

a = hwidth * factor; 
b = hheight * factor; 

[x, y] = meshgrid(1:hwidth, 1:hheight); 

% simple ellipse equation gets us part three of your mask 
bottom_right_mask = (((x.^2)/a^2+(y.^2)/b^2)<=1); 

% flip to get the remaining ones 
top_right_mask = flipud(bottom_right_mask); 
bottom_left_mask = fliplr(bottom_right_mask); 
top_left_mask = flipud(bottom_left_mask); 

mask = [top_left_mask, top_right_mask; ... 
     bottom_left_mask, bottom_right_mask]; 

multichannel_mask = repmat(mask,[1 1 3]); 

ROI = img; 
ROI(multichannel_mask==0) = 0; 

figure; 
imshow(ROI); 
Смежные вопросы