Hi everybody. I have used matlab answers in order to use imellipse to crop an image (I have attached the original and cropped images). it works great for my data. Now, I want to complement only the ROI region (make black intensities white and white ones black) and keep the background black as well. Is there any solution for that?
% 1) Open the image file as a workspace variable:
img = rgb2gray(imread('4-chamber view41.jpg'));
% 2) Display the image and store its handle:
h_im = imshow(img);
% 3) Create an ellipse defining a ROI:
e = imellipse(gca,[265 160 273 381]);
% 4) Create a mask from the ellipse:
BW = createMask(e,h_im);
% 4a) (For color images only) Augment the mask to three channels:
BW(:,:,2) = BW;
BW(:,:,3) = BW(:,:,1);
% 5) Use logical indexing to set area outside of ROI to zero:
ROI = img;
ROI(BW == 0) = 0;
% 6) Display extracted portion:
figure, imshow(ROI);
BR

 Akzeptierte Antwort

Image Analyst
Image Analyst am 15 Jun. 2019

0 Stimmen

Try this:
img = 255 - img; % Invert gray levels.
img(~BW) = 0; % Make sure background stays black.

6 Kommentare

talayeh ghodsi
talayeh ghodsi am 15 Jun. 2019
sorry i have some errors, maybe i put it in wrong place in the code
is it possible for you to put it in the right place in the code?
thanks
Did you try this?
% 1) Open the image file as a workspace variable:
img = rgb2gray(imread('4-chamber view41.jpg'));
% 2) Display the image and store its handle:
subplot(1, 2, 1);
h_im = imshow(img);
% 3) Create an ellipse defining a ROI:
e = imellipse(gca,[265 160 273 381]);
% 4) Create a mask from the ellipse:
BW = createMask(e,h_im);
img = 255 - img; % Invert gray levels.
img(~BW) = 0; % Make sure background stays black.
subplot(1, 2, 2);
imshow(img);
0000 Screenshot.png
talayeh ghodsi
talayeh ghodsi am 15 Jun. 2019
thanks a lot
talayeh ghodsi
talayeh ghodsi am 15 Jun. 2019
sorry and do you know how can i rotate only the complemented region (ROI)? for example rotate your right image 45degree clockwise?
rotatedImage = imrotate(img, 45); % Or -45.
talayeh ghodsi
talayeh ghodsi am 16 Jun. 2019
thanks a lot

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by