matlab - Grayscale segmentation/feature extraction/blob detection? -
i'm trying find starting point, can't seem find right answer. i'd grateful guidance. don't know proper terminology, hence title.
- i took image of bag black background behind it.
- and want extract bag, similar this.
- and if possible, find center, this.
essentially, want able extract blob of pixels , find center point.
i know these 2 separate questions, figured if can latter, can first. using matlab, write own code , not use image processing functions, edge(). methods/algorithms can use? papers/links nice (:
well, assuming image consists of black background , bag inside it, common way perform you're asking threshold image, find centroid of of white pixels.
i did google search , closest thing can think of matches want looks this:
this image rgb reason, though it's grayscale we're going convert grayscale. i'm assuming can't use built-in matlab functions , rgb2gray
out. can still implement though rgb2gray
implements smpte rec. 709 standard.
once read in image, can threshold image , find centroid of of white pixels. can done using find
determine non-zero row , column locations , you'd find mean of both of them separately. once that, can show image , plot red circle centroid located. such:
im = imread('http://ak.picdn.net/shutterstock/videos/3455555/preview/stock-footage-single-blank-gray-shopping-bag-loop-rotate-on-black-background.jpg'); %// convert colour image grayscale im = double(im); im = 0.299*im(:,:,1) + 0.587*im(:,:,2) + 0.114*im(:,:,3); im = uint8(im); thresh = 30; %// choose threshold here %// threshold image im_thresh = im > thresh; %// find non-zero locations [rows,cols] = find(im_thresh); %// find centroid mean_row = mean(rows); mean_col = mean(cols); %// show image , centroid imshow(im); hold on; plot(mean_col, mean_row, 'r.', 'markersize', 18);
when run above code, get:
not bad! next concern case of handling multiple objects. have intelligently determined, code detects 1 object. case of multiple objects, we're going have different. need identify of objects in image id. means need create matrix of ids each pixel in matrix denotes which object object belongs to. after, iterate through each object id , find each centroid. performed creating mask each id, finding centroid of mask , saving result. known finding connected components.
regionprops
common way in matlab, want implement yourself, defer post wrote while ago on how find connected components of binary image:
how find connected components in binary image in matlab?
mind you, algorithm not efficient 1 may take few seconds, i'm sure don't mind wait :) let's deal case of multiple objects now. found image on google:
http://cdn.c.photoshelter.com/img-get2/i0000dqehphmgs.w/fit=1000x750/84483552.jpg
we'd threshold image normal, what's going different performing connected components analysis, iterate through each label , find centroid. however, additional constraint i'm going enforce we're going check area of each object found in connected components result. if it's less number, means object attributed quantization noise , should skip result.
therefore, assuming took code in above linked post , placed function called conncomptest
has following prototype:
b = conncomptest(a);
so, take code in referenced post, , place function called conncomptest.m
function header such that:
function b = conncomptest(a)
where a
input binary image , b
matrix of ids, this:
im = imread('http://cdn.c.photoshelter.com/img-get2/i0000dqehphmgs.w/fit=1000x750/84483552.jpg'); im = double(im); im = 0.299*im(:,:,1) + 0.587*im(:,:,2) + 0.114*im(:,:,3); im = uint8(im); thresh = 30; %// choose threshold here %// threshold image im_thresh = im > thresh; %// perform connected components analysis labels = conncomptest(im_thresh); %// find total number of objects in image num_labels = max(labels(:)); %// find centroids of each object , show image figure; imshow(im); hold on; idx = 1 : num_labels %// find ith object mask mask = labels == idx; %// find area arr = sum(mask(:)); %// if area less threshold %// don't process object if arr < 50 continue; end %// else, find centroid %// find non-zero locations [rows,cols] = find(mask); %// find centroid mean_row = mean(rows); mean_col = mean(cols); %// show image , centroid plot(mean_col, mean_row, 'r.', 'markersize', 18); end
we get:
Comments
Post a Comment