Locating a group of pixels in an image - matlab

I'm doing some low level image processing project and I need to locate the position of an object. In this case it's a comet. I played a bit with the thresholds and got a pretty much binary image, but what I need next is to locate a fixed point in the middle of the large group of white pixels so it can be traced or something. Any idea on how to do that?
Here's a screen shot of what I got.
Link to the photo
And is there a way to filter out all the white dots all over the picture? Some kind of function to give all small groups value of "0"?
Thanks in advance!

Maybe you are looking for this -
Code
%%// Read in image and convert to binary
img = imread(IMAGE_FILEPATH);
BW = im2bw(img);
%%// Get only the comet blob, which is the biggest blob
[L, num] = bwlabel(BW);
counts = sum(bsxfun(#eq,L(:),1:num));
[~,ind] = max(counts);
BW = (L==ind);
%%// Find the centroid of the comet blob
stats = regionprops(BW, 'Centroid');
center_point = stats.Centroid
Output
center_point =
56.7471 131.9373
Note: There's another question - Select largest object in an image that relates to this case.

MATLAB regionprops is your friend, and your image is good enough for what you want to do.
regionprops does just what you need (you get the X,Y of the center of mass). Since it also gives the area, you can filter results and only retain the largest object in your image.

Your problem seems to be solved with regionprops, as others have proposed already, but if you're interested, in general, in eliminating small white artefacts in an image:
You could apply one or multiple steps of erosion to your binary image, eliminating small white spots (but also decreasing the size of your comet!).

you can use a median filter or a morphological opening filter to remove small white regions. you'll have to experiment and pick the right size for the filter kernel. assuming you have a cleaner image after filtering, you can perform a labeling and then do a regionprops to get the centroid of the blob.

Related

Imfill holes won't fill every segment I need. Is there anything I can do about this? (avoiding graythresh)

hope you might have a suggestion for my struggle (matlab). This is the result of the Imfill holes function, but it left a lot of segments unfilled. Is there anything I can attempt to fix this?
imfill(BW1,'holes')
You may use bwmorph with 'bridge' argument for bridging small gaps before imfill.
'bridge'
Bridges unconnected pixels, that is, sets 0-valued pixels to 1 if they have two nonzero neighbors that are not connected.
Here is a code sample:
I = imread('holes.jpg');
% Remove top and bottom "white frame".
I = I(7:end-6, :);
% Convert to binary image with manual threshold (could be that binarize is required only due to JPEG artifacts).
I = imbinarize(I, 0.3);
% Bridges unconnected pixels (morphological operation).
J = bwmorph(I, 'bridge');
% Fill holes
K = imfill(J, 'holes');
Remarks:
Please consider posting the original image (before fill).
Consider posting images in PNG format instead of JPEG - JPEG compression looses information and creates compression artifacts.
The image you have posted has a "white frame", that is probably not part of the original image. I decided to keep the left and right parts for filling the shapes next to the borders.
Result:

Which kind of filtering is used in SPCImage for binning?

I was wondering if anyone knows which kind of filter is applied by SPCImage from the Becker & Hickl system.
I am taking some FLIM data with my system and I want to create the lifetime images. For doing so I want to bin my images in the same way as it does SPCImage, so I can increase my SN ratio. The binning goes like 1x1, 3x3, 5x5, etc. I have created the function for doing a 3x3 binning, but each time it gets more complicated...
I want to do it in MATLAB, and maybe there is already a function that can help me with this.
Many thanks for your help.
This question is old, but for anyone else wondering: You want to sum the pixels in an (2M+1) x (2M+1) neighborhood for each plane (M integer). So I am pretty sure you can go about the problem by treating it like a convolution.
#This is your original 3D SDT image
#I assume that you have ordered the image with spatial dimensions along the
#first and second and the time channels are the third dimension.
img = ... #<- your 3D image goes here
#This describes your filter. M=1 means take 1 a one pixel rect around your
#center pixel and add the values to your center, etc... (i.e. M=1 equals a
#total of 3x3 pixels accumulated)
M=2
#this is the (2D) filter for your convolution
filtr = ones(2*M+1, 2*M+1);
#the resulting binned image (3D)
img_binned = convn(img, filtr, 'same');
You should definitely check the result against your calculation, but it should do the trick.
I think that you need to test/investigate image filter functions to apply to this king of images Fluorescence-lifetime imaging microscopy.
A median filter as showing here is good for smoothering things. Or a weihgted moving average filter where applied to the image erase de bright spots and only are maintained the broad features
So you need to review of the digital image processing in matlab

Autonomous seam detection in Images on matlab

I'm trying to detect seams in welding images for an autonomous welding process.
I want to find pixel positions of the detected line (the red line in the desired image) in the original image.
I used the following code and finally removed noise from the image to reach the result below.
clc,clear,clf;
im = imread('https://i.stack.imgur.com/UJcKA.png');
imshow(im);title('Original image'); pause(0.5);
sim = edge(im, 'sobel');
imshow(sim);title('after Sobel'); pause(0.5);
mask = im > 5;
se = strel('square', 5);
mask_s = imerode(mask, se);
mask(mask_s) = false;
mask = imdilate(mask, se);
sim(mask) = false;
imshow(sim);title('after mask');pause(0.5);
sim= medfilt2(sim);
imshow(sim);title('after noise removal')
Unfortunately there is nothing remaining in the image to find the seam perfectly.
Any help would be appreciated.
Download Original image.
You need to make your filter more robust to noise. This can be done by giving it a larger support:
filter = [ones(2,9);zeros(1,9);-ones(2,9)];
msk = imerode(im > 0, ones(11)); % only object pixels, discarding BG
fim =imfilter(im,filter);
robust = bwmorph((fim>0.75).*msk,'skel',inf); % get only strong pixels
The robust mask looks like:
As you can see, the seam line is well detected, we just need to pick it as the largest connected component:
st = regionprops(bwlabel(robust,8), 'Area', 'PixelList');
[ma mxi] = max([st.Area]); % select the region with the largest area
Now we can fit a polygon (2nd degree) to the seem:
pp=polyfit(st(mxi).PixelList(:,1), st(mxi).PixelList(:,2), 2);
And here it is over the image:
imshow(im, 'border','tight');hold on;
xx=1:size(im,2);plot(xx,polyval(pp,xx)+2,'r');
Note the +2 Y offset due to filter width.
PS,
You might find this thread relevant.
Shai gives a great answer, but I wanted to add a bit more context about why your noise filtering doesn't work.
Why median filtering doesn't work
Wikipedia suggests that median filtering removes noise while preserving edges, which is why you might have chosen to use it. However, in your case it will almost certainly not work, here's why:
Median filtering slides a window across the image. In each area, it replaces the central pixel with the median value from the surrounding window. medfilt2 uses a 3x3 window by default. Let's look at a 3x3 block near your line,
A 3x3 block around [212 157] looks like this
[0 0 0
1 1 1
0 0 0]
The median value is 0! So even though we're in the middle of a line segment, the pixel will be filtered out.
The alternative to median filtering
Shai's method for removing noise instead finds the largest connected group of pixels and ignores smaller groups of pixels. If you also wanted to remove these small groups from your image, Matlab provides a filter bwareaopen which removes small objects from binary images.
For example, if you replace your line
sim= medfilt2(sim);
with
sim= bwareaopen(sim, 4);
The result is much better
Alternative edge detectors
One last note, Shai uses a horizontal gradient filter to find horizontal edges in your image. It works great because your edge is horizontal. If you edge will not always be horizontal, you might want to use another edge detection method. In your original code, you use Sobel, but Matlab provides many options, all of which perform better if you tune their thresholds. As an example, in the following image, I've highlighted the pixels selected by your code (with bwareaopen modification) using four different edge detectors.

Unite endpoints of edge with line

I'm trying to make an object recognition program using a k-NN classifier. I've got a bunch of images for the training part of the classifier and a bunch of images to recognize. Those images are in grayscale and there's an object (only its edge) per image. I need to calculate their center of mass so I use
img=im2bw(img)
and then regionprops(img,'centroid').
The problem is that some of those edges aren't closed so regionprops doesn't work then. I tried eroding the image (the edge is black, white background) but the endlines of those edges are too apart from eachother. I tried using bwmorph function to do so but still can't make it work.
Any ideas?
EDIT
I'm adding some images in case anyone wants to try:
Use morphological operation
Use a closing operation to make your structures filled.
1. As first step prepare your image data
im = imread('your image.jpg');
% Get first channel as gray scale information
im = im(:,:,1);
% Threshold it for simplicyty, you may work on grayscale too.
im1 = logical(im > 128);
2. Use a simple block shaped structuring element
The structuring element is defined by:
strel=ones(3,3);
You may use disk shaped elements or whatever gives the best result to you.
3. Apply structuring element a couple of times
Apply the strel a couple of times with an erosion operator to your original image to close your figure:
for i=1:20
im1 = imerode(im1,strel);
end
4. Dilate the image to get back to original shape
Next step is to dilate the image to get back to your original outer shape:
for i=1:20
im1 = imdilate(im1,strel);
end
Final result
The final result should be suitable to get a sufficiently precise center or gravity.

Count the amount of closed contours in an image - MATLAB

I am struggling to find a good contour detection function that would count the number of contour in bw images that I have processed using some previous tools. As you can see, my profile picture is an example of such images,
,
In this image, ideally, I wish to have a function which counts four closed contour.
I don't mind if it also detects the really tiny contours in between, or the entire shape itself as extra contours. As long as it counts the medium sized ones, I can fix the rest by applying area threshold. My problem is that any function I have tried detects only one contour - the entire shape, as it cannot separate it to the su-conours which are connected to one another.
Any suggestions?
Here is my shot at this, although your question might get closed because it's off-topic, too broad or a possible duplicate. Anyhow I propose another way to count the number of contours. You could also do it using bwboundaries as was demonstrated in the link provided by #knedlsepp in the possible duplicate. Just for the sake of it here is another way.
The idea is to apply a morphological closure of your image and actually count the number of enclosed surfaces instead instead of contours. That might end up being the same thing but I think it's easier to visualize surfaces.
Since the shapes in your image look like circle (kind of...) the structuring element used to close the image is a disk. The size (here 5) is up to you but for the image you provided its fine. After that, use regionprops to locate image regions (here the blobs) and count them, which comes back to counting contours I guess. You can provide the Area parameter to filter out shapes based on their area. Here I ask the function to provide centroids to plot them.
Whole code:
clear
clc
close all
%// Read, threshold and clean up the image
Im = im2bw(imread('ImContour.png'));
Im = imclearborder(Im);
%// Apply disk structuring element to morphologically close the image.
%// Play around with the size to alter the output.
se = strel('disk',5);
Im_closed = imclose(Im,se);
%// Find centroids of circle-ish shapes. Youcan also get the area to filter
%// out those you don't want.
S = regionprops(~Im_closed,'Centroid','Area');
%// remove the outer border of the image (1st output of regioprops).
S(1) = [];
%// Make array with centroids and show them.
Centro = vertcat(S.Centroid);
imshow(Im)
hold on
scatter(Centro(:,1),Centro(:,2),40,'filled')
And the output:
So as you see the algorithm detected 5 regions, but try playing a bit with the parameters and you will see which ones to change to get the desired output of 4.
Have fun!