Crop a image using 'imfreehand' and move the cropped region to the center of black background

I've cropped an ROI with imfreehand() and plotted the center of it using mean (position of cropped region). Then I've plotted the center of completely black image. For the resulting image to have the cropped image at center of black background, is it OK to map the marked centers of both images, or get positions for moving the region manually? I want to do this for multiple images, so is it possible to apply any formula or function? Also I've attached example images and sample output. Kindly help me in this.
I've tried also tried using circular shift, circshift(), for moving the cropped portion to the center of black background, but it is not working. Applying a circular shift made the ROI to move like half portion up an remaining down the center of black image. Why did it appear so?
Original Image:
ROI with centroid marked:
Center of black image:
Desired output image:
Thank you and have a Good Day.

 Accepted Answer

It's not obvious, but you can use imwarp() instead of circshift. See this demo:
% Shifts / translates an image to the right by 500 pixels and down by 200 pixels.
% Area shifted in is black.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
grayImage = imread('concordorthophoto.png');
[rows, columns, numberOfColorChannels] = size(grayImage);
subplot(1, 2, 1);
imshow(grayImage);
axis on;
title('Original Image', 'fontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
deltaX = 500; % Shift x by 500 pixels.
deltaY = 200; % Shift y by 200 pixels.
D = zeros(rows, columns, 2);
D(:,:,1) = -deltaX; % Shift x by 500 pixels.
D(:,:,2) = -deltaY; % Shift x by 200 pixels.
warpedImage = imwarp(grayImage, D);
subplot(1, 2, 2);
imshow(warpedImage);
axis on;
title('Shifted Image', 'fontSize', fontSize);
So just mask the image in place (see attached demo script freehand_masking_demo.m), then do the shifting with imwarp().

6 Comments

Sai's "Answers" moved here:
Hi Image Analyst. Thank you for your reply. I tried it but it resulted in resized portion of cropped image. Also, I'm unable to implement imwrap() in Matlab 2011. I want to place the original cropped region in the center, not the stretched version of it. I tried using imline( ) to get the position co-ordinates for moving the cropped region to center, but that also didn't workout. Kindly help me to place the cropped portion, without re-sizing, to the center.
And one more thing is that, the size of the image on which the ROI was cropped free hand was 320*240 pixels and the size of that black image on which the cropped portion is to be centralized is also 320*240 pixels. Can the centroid of cropped region and the center of black image be matched to achieve centralization?
The demo does not resize or stretch the pasted image. Post your code and image(s).
Hi Image Analyst. Finally, I've somewhat got what I needed by modifying my own program. Yours did work for me, but there were few problems which I couldn't solve in it.
Thank you once again and Good Day.
I'd appreciate it if you let me know what the problems were, because I post this frequently and it seems to work fine for me. Like I said, there is no resizing done (see bottom left two images), though imshow() will automatically scale the image to best fit it in the axes, as you can see from the axes tick mark labels in the lower right image.
Hi Image Analyst. Yes, I do agree and understand that the cropped portion was not resized, but I've got confused with the axes showing different range rather than its original position. I've attached my output for the program you've linked for your reference. Even for me the resultant image showed different which slightly (sort of) confused me. Is it possible to change the range of axes being displayed, automatically? Thank you for your kind reply and Good Day.
If you don't want it to automatically magnify the image to fill the axes space available for it, you can use a scroll panel. See attached demo.

Sign in to comment.

More Answers (0)

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Asked:

Sai
on 28 Oct 2015

Commented:

on 2 Nov 2015

Community Treasure Hunt

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

Start Hunting!