how to detect center of an object in an image and then crop the original image? using original image and green outline region

8 views (last 30 days)
Hello.
I need to detect an object (suspicious area in an image). Then I need to find its centroid and then crop it the image into 256x256 pixels using image centroid as the centre of bounding box.
img = imread('1_245_original.jpg');
% colorspace
jmg = rgb2gray(img);
jm = mat2gray(jmg(:,:,1));
jm = imcomplement(jm);
% thresh
bw = imbinarize(jm, graythresh(jm));
% filter noise
bw = imopen(bw, strel('disk', 5));
bw = imfill(bw, 'holes');
% label every target
[L,num] = bwlabel(bw);
stats = regionprops(L);
figure; imshow(img, []);
for i = 1 : num
% get rect
recti = stats(i).BoundingBox;
% get cen
ceni = stats(i).Centroid;
% crop image
imi = imcrop(img, round(recti));
ims{i} = imi;
% rect and cen
hold on; rectangle('Position', recti, 'EdgeColor', 'c', 'LineWidth', 2);
plot(ceni(1), ceni(2), 'yp', 'MarkerFaceColor', 'y', 'MarkerSize', 20);
end
I have tried out this code, but the cropping area was not in 256x256 pixels. and I want it to save at folder after detect the bounding box. Where can I put that code? Thanks.
  1 Comment
Image Analyst
Image Analyst on 20 May 2024
You've asked this before. Finding dark regions is not a problem, nor is cropping them and resizing them. The problem is identifying which of the many dark regions in the image is the specific one you want, and you haven't specified what it is about that dark blob that distinguishes it from all the other dark blobs. Yes, I know you've said that it is diseased tissue but how does it LOOK different than all the others?

Sign in to comment.

Answers (1)

Aastha
Aastha on 25 Sep 2024
As I understand, for each proposed region you want to create a bounding box of size 256x256 pixels around the centroid and save it to disk. This can be accomplished by making the following additions to the given code:
For each region of proposals, the variable recti provides a tuple of the form (x, y, w, h), where (x, y) are the coordinates of the top-left corner, and (w, h) represent the width and height of the bounding box, respectively. To adjust the bounding box to a size of 256x256, you can make both the width and height equal to 256. This can be achieved by setting the width and height to 256 and adjusting the top-left corner (x, y) accordingly to keep the box centred around the centroid. You may refer to the code snippet below to do this:
ceni=stats(i).Centroid;
topx=max(0,ceni(1)-128); % account for out of bounds index
topy=max(0,ceni(2)-128); % account for out of bounds index
recti=[topx topy 256 256];
imi = imcrop(img, round(recti));
For more information on “imcrop and “max” function, you may refer to the link of MathWorks documentation mentioned below:
To save the cropped image to disk, you can use the "imwrite" function in MATLAB as follows:
imwrite(img, 'save_filename.jpg")
In this way, you can crop and make a bounding box of size 256x256 centred around the centroid and save it to disk.
You may refer to the documentation of "imwrite" function for more details: 
I hope this is helpful!

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!