How to count number of matching object in image in Matlab - matlab

I have a task where I should count numbers of suits(diamonds, clubs, ...) in a set of playing cards image. I have created a template sub-image from my original image for diamond for example, using imcrop in Matlab. I have also converted both Original or target Image in grayscale.
I'm trying to find the match of the sub-image in the target image and counts the corresponding diamonds in the target image.
Does anyone have a suggestion?
I try to use normxcorr2 I got a plot where I can see the area with highest peak, but I don't have any ideas how to compute this.
Any suggestions of algorithms.
Thank you.

Have a look at method A) in Detect repetitive pixel patterns in an image and remove them using matlab (Disclaimer: I'm the author). Delete the rect line and replace the variable template with your (BW) template. Skip the last 3 commands and instead just count how many peaks there are:
idx = bwmorph(idx,'shrink',inf);
numberOfObjects = sum(idx)
You obviously will have to adjust some values greatly to get a good result - pattern detection isn't trivial.

Related

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

Image Processing Q: Separate/segment an image

need some help here on image processing. I'm using Matlab and try to segment the following figure based on the two major peaks (in yellow). The color yellow means higher value and blue means low value (on z-axis, or image color from 0 to 1 for your convenience). The ideal cut is roughly the line from point (1,75) to (120,105). But I want a systematic way to derive this rather than by observation.
My intuition was to first identify the two peaks (based on this), and then classify each point/pixel on this figure to the two peaks (the metric here is to compute the shortest Euclidean distance to the edge of the two peaks).
And I end up with the following fig.
As you can see, the cut is pretty much a straight line, which I'm not quite satisfied. Maybe I can use the orientation of the peak circle and somehow tilt the line.. but I'm not sure how to do so? Any clues? Thanks.
This is an Image segmentation problem.
you can use GMM Gaussian of Mixture Model to model the image.
in your case the number of components will be 2.
after you model the image by using this mixture, you can find the probability of each pixel P(pixel x belong to the first component or the second component)
check
http://www.mathworks.com/matlabcentral/newsreader/view_thread/272162
http://www.mathworks.com/help/stats/cluster-data-from-mixture-of-gaussian-distributions.html

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!

MatLab Daubechies Filter of colored picture form ground up - not using preset functions

this is my first post. I hope someone can help me getting started. So I've been working on image processing for a class project. We started out easy by creating a Haar transform function from scratch, which was pretty straight forward and then applying it to preset pictures such as cameraman.tif. However, that sample picture is on a grayscale and a square matrix.
Now we made a big leap and we're supposed to apply the filter to any rectangular colored picture. I understand that if I imread(picture) it will create an array matrix with three values representing red, green and blue (RGB). I also understand that principally speaking I have to use a for loop to go through each channel of color. However, it trips me on how to create it for the different channels and put the image back together.
I went through some awesome answers here and was able to reconstruct the woman and fruit basket example, but unfortunately they all use the preset DWT function, which I must not.
The final code should go like:
1)Ask the user how many iterations are desired:
pic = input('What picture would you like? Type "1" for an example or a URL ');
2) Ask the user about the threshold
t = input('Threshold?');
3) Ask the user how many iterations (1-3)
i = input('How many iterations do you want? Choose 1-3. ');
As a sample image, I chose a rectangular bitmap, because it hasn't been compressed yet and using "1" as an easy identifier:
if pic ==1;
A = imread ('http://readingeagle.com/BlogUploads/11/Birds%20in.bmp');
else A = imread(pic) ;
end
%[m,n]=Matrix dimensions, c=channels
[m,n,c]=size (A) ;
%plot original
imshow (A);
Now we have to perform the Daubchechies Wavelet Transform with however many iterations the user specified while setting the values outside of the Threshold's absolute value to zero. Before reconstructing the image.
I hope I gave enough information, please correct me if I went wrong already (I hope not). MY code starts breaking every time I implement loops and try to address the channels. Just for reference purposes here is the Haar transform that worked for the original cameraman.tif image:
%HWTM - Haar Wavelet Transform Matrix
function W = HWTM(N)
while mod(N,2) ~= 0;
N = N-1;
end
W=zeros(N);
for k=1:N/2
W(k,2*k)=1/2;
W(k,2*k-1)=1/2;
W(N/2+k,2*k)=1/2;
W(N/2+k,2*k-1)=-1/2;
end
end
Thanks a million!

How to recognize this simple capcha?

alt text http://internationalpropertiesregistry.com/Server/showFile.php?file=%2FUpload%2F02468.gif358455ebc982cb93b98a258fc4d6ee60.gif
Is there a simple solution in MATLAB?
Easy answer: NOPE
For very simple: read the image as grayscale, threshold, clean up and run it through an ocr program.
%# read the image
img = imread('http://internationalpropertiesregistry.com/Server/showFile.php?file=%2FUpload%2F02468.gif358455ebc982cb93b98a258fc4d6ee60.gif');
%# threshold
bw = img < 150;
%# clean up
bw = bwareaopen(bw,3,4);
%# look at it - the number 8 is not so pretty, the rest looks reasonable
figure,imshow(bw)
Then, figure out whether there is an OCR program that can help, such as this one
For even simpler:
%# read the image
[img,map] = imread('http://internationalpropertiesregistry.com/Server/showFile.php?file=%2FUpload%2F02468.gif358455ebc982cb93b98a258fc4d6ee60.gif');
%# display
figure,imshow(img,map)
%# and type out the numbers. It's going to be SO much less work than writing complicated code
For a simple one like that you can probably just run a median filter and ocr.
A median filter will, for every pixel in the image, look at the area around it, usually a 3x3 or 5x5 pixel area, determine the median pixel value in that area and set the pixel to that median value. In a block of the same colour nothing will happen, the whole area is the same colour as the pixel under consideration so the median i sthe same as the current value (or at least almost the same permitting slight colour variations. On the other hand noise pixels, i.e. a single pixel with a differently coloured area around it will simply disappear, since the median value of the area will be the colour of all the pixels around the noise pixel.
As for the ocr, or optical character recognition, I'd just use an existing program/library, it would certainly be possible to write an ocr algorithm in Matlab, but would be a much bigger exercise than writing a simple algorithm in an hour. You'd first need to read up on ocr techniques and algorithms.
Clean up the image
Separate each character into distinct images
Compare each character against a set of reference characters
The best matches are the most likely original character
You may consult this post. I succeeded in cracking a simpler CAPTCHA with the illustrated approach.