Matching closest image in openCV? - image

This is the dilenma I'm having. For my application, I need to match an image L1 with the matching one L2 in a set of images. L1 and L2 are the exact same image, except L1 is much smaller (It will need to be upscale?), and could be artifacted a little on the edges, but nevertheless, they are from the exact same source image. Color DOES matter, in that using color information will remove possible ambiguities between the current image and the one it is to be matched with. Using OpenCV (or perhaps there may be a better alternative?), what is the best way to find the matching image (L2).
To reiterate, the image to be matched with is not rotated or distorted in anyway, only resized.
I guess there would be a function which rates how close the image to be matched is to all of the images in the set provided. Then we choose the one with the highest rating as the match. I'm not sure as to how to compare the images though. Any help would be great. Thanks.

Go to github and check out opencv-master\samples\cpp\matcher_simple.cpp(or matching_to_many_images.cpp)
not only can it satisfy your need but it also works for images with perspective distortion (eg. rotation, affine transformation and illumination variation). simply put, it's very robust.
but SIFT and SURF are patented, you might not be able to use it for commercial applications, which sucks. but there are many alternatives, just google around!

OpenCV has a tutorial on similarity measurement for images.
You will need to upscale L1 before doing the comparison, or downscale L2. If you are comparing L2 against lots of images it probably makes more sense to scale L2 down (because you then don't have to call resize for every image being compared against, and there are fewer pixels to be compared).
e.g.
cv::Mat L1 = ...;
cv::Mat L2 = ...;
cv::Mat L2small;
cv::resize(L2, L2small, L1.size());
double pnsr = getPSNR(L1, L2small);
// where code for getPSNR() is in the tutorial

I think you may be able to use something similar to Bag-of-words model used to measure document similarity. Take a look at this: link
I'm reproducing the equation below:
G = X'*X
where X = [x1 x2 ... xn]
In your case, use the normalized histogram of the image as vector xi.
I think you wouldn't have to resize the images in this approach and it would be faster.
EDIT
I tried this in Matlab using some sample images provided in opencv samples:
im1 = imread('baboon.jpg');
im2 = imread('board.jpg');
im3 = imread('fruits.jpg');
im4 = imread('fruits - small.jpg'); % fruits.jpg scaled down 25% using mspaint
% using grayscale for simplicity
gr1 = rgb2gray(im1);
gr2 = rgb2gray(im2);
gr3 = rgb2gray(im3);
gr4 = rgb2gray(im4);
[cnt_baboon, x] = imhist(gr1);
[cnt_board, x] = imhist(gr2);
[cnt_fruits, x] = imhist(gr3);
[cnt_fruits_small, x] = imhist(gr4);
% X: not normalized
X = [cnt_baboon cnt_board cnt_fruits cnt_fruits_small];
H = X'*X;
N = sqrt(diag(H)*diag(H)');
% normalize. this would be faster
G = H./N
The resulting G matrix:
G =
1.0000 0.8460 0.7748 0.7729
0.8460 1.0000 0.8741 0.8686
0.7748 0.8741 1.0000 0.9947
0.7729 0.8686 0.9947 1.0000
You can see that G(3,4) (and G(4,3)) are very close to 1.

i think you are looking for histogram matching. There are inbuilt functions for histogram matching, for example bhattacharya distance etc, and they don't require both of your images to be of same size also.
Just check this link on opencv site,
link

Related

cut away the black frame of images using MatLab

For my master thesis I have to examine several similarly structured images with Matlab. I have already coded the actual evaluation. My problem is that the images have a black frame of different thickness. This will cause the evaluation to be falsified. So far I cut the border by hand. So I wanted to ask if someone could help me cut this frame using a Matlab code.
Below is an example picture,
I tried to solve this problem with the edge-plug in (https://de.mathworks.com/help/images/ref/edge.html) but it didn´t worked.
Maybe you can give me some advice
I wouldn't be using the edge detection as you have linked to in your question.
Rather, I would focus on reducing the dimensions of the image and analyzing the "reduced image". In the following code you can see an example of reducing the image into two representative lines, and analyzing the line to detect the specimen.
% Load the image.
Img = imread('pfi36.png');
% Level to pass for the "edge" detection.
lvl = 50;
% Add a buffer around the edges.
buf = 10;
% Reduce the Image to 1D along the x and y driections respectively.
lrData = mean(Img,1);
tdData = mean(Img,2);
% Find the indices of the edges.
indL = find(lrData>lvl,1,'first');
indR = find(lrData>lvl,1,'last');
indB = find(tdData>lvl,1,'last');
% Show the Cropped image.
newImg = Img(1:(indB+buf),(indL-buf):(indR+buf));
imshow(newImg);

MATLAB: layer detection, vector combination and selection by tortuosity/arclength

I have a greyscale image similar to the one below that I have achieved after some post-processing steps (image 0001). I would like a vector corresponding to the bottom of the lower bright strip (as depicted in image 0001b). I can use im2bw with various thresholds to achieve the vectors in image 0002 (the higher the threshold value the higher the tendency for the vector line to blip upwards, the lower the threshold the higher the tendency for the line to blip downwards)..and then I was thinking of going through each vector and measuring arclength over some increment (maybe 100 pixels or so) and choosing that vector with the lowest arclength...and adding that 100 pixel stretch to the final vector, creating a frankenstein-like vector using the straightest segments from each of the thresholded vectors.. I should also mention that when there are multiple straightish/parallel vectors, the top one is the best fit.
First off, is there some better strategy I should be employing here to find that line on image 0001? (this needs to be fast so some long fitting code wouldn't work). If my current Frankenstein's monster solution works, any suggestions as to how to best go about this?
Thanks in advance
image=im2bw(image,0.95); %or 0.85, 0.75, 0.65, 0.55
vec=[];
for v=1:x
for x=1:z
if image(c,v)==1
vec(v)=c;
end
end
end
vec=fastsmooth(vec,60,20,1);
Here is the modified version of what I originally did. It works well on on your images. If you want subpixel resolution, you can implement an active contour model with some fitting function.
files = dir('*.png');
filenames = {files.name};
for ifile=1:length(filenames)
%%
% read image
im0 = double(imread(filenames{ifile}));
%%
% remove background by substracting a convolution with a mask
lobj=100;
convmask = ones(lobj,1)/lobj;
im=im0-conv2(im0,convmask,'same');
im(im<0)=0;
imagesc(im);colormap gray;axis image;
%%
% use canny edge filter, alowing extremely weak edge to exist
bw=edge(im,'canny',[0.01,0.3]);
% use close operation on image to close gaps between lines
% the kernel is a flat rectangular so that it helps to connect horizontal
% gaps
se=strel('rectangle',[10,30]);
bw=imdilate(bw,se);
% thin the lines to be single pixel line
bw=bwmorph(bw,'thin',inf);
% connect H bridge
bw=bwmorph(bw,'bridge');
imagesc(bw);colormap gray;axis image;
%% smooth the image, find the decreasing region, and apply the mask
imtmp = imgaussfilt(im0,3);
imtmp = diff(imtmp);
imtmp = [imtmp(1,:);imtmp];
intensity_decrease_mask = imtmp < 0;
bw = bw & intensity_decrease_mask;
imagesc(bw);colormap gray;axis image;
%%
% find properties of the lines, and find the longest lines
cc=regionprops(bw,'Area','PixelList','Centroid','MajorAxisLength','PixelIdxList');
% now select any lines that is larger than eighth of the image width
cc=cc([cc.MajorAxisLength]>size(bw,2)/8);
%%
% select lines that has average intensity larger than gray level
for i=1:length(cc)
cc(i).meanIntensity = mean(im0(sub2ind(size(im0),cc(i).PixelList(:,2), ...
cc(i).PixelList(:,1) )));
end
cc=cc([cc.meanIntensity]>150);
cnts=reshape([cc.Centroid],2,length(cc))';
%%
% calculate the minimum distance to the bottom right of each edge
for i=1:length(cc)
cc(i).distance2bottomright = sqrt(min((cc(i).PixelList(:,2)-size(im,1)).^2 ...
+ (cc(i).PixelList(:,1)-size(im,2)).^2));
end
% select the bottom edge
[~,minindex]=min([cc.distance2bottomright]);
bottomedge = cc(minindex);
%% clean up the lines a little bit
bwtmp = false(size(bw));
bwtmp(bottomedge.PixelIdxList)=1;
% find the end points to the most left and right
endpoints = bwmorph(bwtmp, 'endpoints');
[endy,endx] = find(endpoints);
[~,minind]=min(endx);
[~,maxind]=max(endx);
pos_most_left = [endx(minind),endy(minind)];
pos_most_right = [endx(maxind),endy(maxind)];
% select the shortest path between left and right
dists = bwdistgeodesic(bwtmp,pos_most_left(1),pos_most_left(2)) + ...
bwdistgeodesic(bwtmp,pos_most_right(1),pos_most_right(2));
dists(isnan(dists))=inf;
bwtmp = imregionalmin(dists);
bottomedge=regionprops(bwtmp,'PixelList');
%% plot the lines
imagesc(im0);colormap gray;axis image;hold on;axis off;
for i=1:length(cc)
plot(cc(i).PixelList(:,1),cc(i).PixelList(:,2),'b','linewidth',2);hold on;
end
plot(bottomedge.PixelList(:,1),bottomedge.PixelList(:,2),'r','linewidth',2);hold on;
print(gcf,num2str(ifile),'-djpeg');
% pause
end
I am not sure this answers your question directly, but I have a lot of experiencing fitting arrays (or matrices in my case) to 3D raster images. We were using relatively low power machines (standard i7 processors 32 gb ram), and had to perform the fitting very quickly (<30 seconds). We also had to validate the fit with a variety of parameters (and again these were 3D rasters fit to a point cloud matrix).
Anyways, the process we used was the fminsearch function internal to Matlab. Documentation can be found here: http://www.mathworks.com/help/optim/functionlist.html
We would start with a plain point-cloud and perform successive manipulations on a per pixel basis to adjust the point-cloud to the raster. Essentially walking through each pixel in the raster to produce the lowest offset between the point cloud and the raster.
I will try to search for some code this afternoon and update my answer, but I might explore this option for your case. I would imagine you could fit a curve to certain pixels (e.g. white pixels) both rapidly and accurately by setting up an optimization function.
I also could help more if I understood your objective better. Are you just trying to fit a line to the high-albedo/white areas?
In the way of example: I can fit a 3D point cloud to the following image by starting with a standard point cloud, the 3D raster, and a minimization function (in this case just RMS error of each individual point in the z axis). Throw an fmin function on there and in a few seconds you get a modified point cloud that fits much better than the standard.

Detect black dots from color background

My short question
How to detect the black dots in the following images? (I paste only one test image to make the question look compact. More images can be found →here←).
My long question
As shown above, the background color is roughly blue, and the dots color is "black". If pick one black pixel and measure its color in RGB, the value can be (0, 44, 65) or (14, 69, 89).... Therefore, we cannot set a range to tell the pixel is part of the black dot or the background.
I test 10 images of different colors, but I hope I can find a method to detect the black dots from more complicated background which may be made up of three or more colors, as long as human eyes can identify the black dots easily. Some extremely small or blur dots can be omitted.
Previous work
Last month, I have asked a similar question at stackoverflow, but have not got a perfect solution, some excellent answers though. Find more details about my work if you are interested.
Here are the methods I have tried:
Converting to grayscale or the brightness of image. The difficulty is that I can not find an adaptive threshold to do binarization. Obviously, turning a color image to grayscale or using the brightness (HSV) will lose much useful information. Otsu algorithm which calculates adaptive threshold can not work either.
Calculating RGB histogram. In my last question, natan's method is to estimate the black color by histogram. It is time-saving, but the adaptive threshold is also a problem.
Clustering. I have tried k-means clustering and found it quite effective for the background that only has one color. The shortage (see my own answer) is I need to set the number of clustering center in advance but I don't know how the background will be. What's more, it is too slow! My application is for real time capturing on iPhone and now it can process 7~8 frames per second using k-means (20 FPS is good I think).
Summary
I think not only similar colors but also adjacent pixels should be "clustered" or "merged" in order to extract the black dots. Please guide me a proper way to solve my problem. Any advice or algorithm will be appreciated. There is no free lunch but I hope a better trade-off between cost and accuracy.
I was able to get some pretty nice first pass results by converting to HSV color space with rgb2hsv, then using the Image Processing Toolbox functions imopen and imregionalmin on the value channel:
rgb = imread('6abIc.jpg');
hsv = rgb2hsv(rgb);
openimg = imopen(hsv(:, :, 3), strel('disk', 11));
mask = imregionalmin(openimg);
imshow(rgb);
hold on;
[r, c] = find(mask);
plot(c, r, 'r.');
And the resulting images (for the image in the question and one chosen from your link):
You can see a few false positives and missed dots, as well as some dots that are labeled with multiple points, but a few refinements (such as modifying the structure element used in the opening step) could clean these up some.
I was curios to test with my old 2d peak finder code on the images without any threshold or any color considerations, really crude don't you think?
im0=imread('Snap10.jpg');
im=(abs(255-im0));
d=rgb2gray(im);
filter=fspecial('gaussian',16,3.5);
p=FastPeakFind(d,0,filter);
imagesc(im0); hold on
plot(p(1:2:end),p(2:2:end),'r.')
The code I'm using is a simple 2D local maxima finder, there are some false positives, but all in all this captures most of the points with no duplication. The filter I was using was a 2d gaussian of width and std similar to a typical blob (the best would have been to get a matched filter for your problem).
A more sophisticated version that does treat the colors (rgb2hsv?) could improve this further...
Here is an extraodinarily simplified version, that can be extended to be full RGB, and it also does not use the image procesing library. Basically you can do 2-D convolution with a filter image (which is an example of the dot you are looking for), and from the points where the convolution returns the highest values, are the best matches for the dots. You can then of course threshold that. Here is a simple binary image example of just that.
%creating a dummy image with a bunch of small white crosses
im = zeros(100,100);
numPoints = 10;
% randomly chose the location to put those crosses
points = randperm(numel(im));
% keep only certain number of points
points = points(1:numPoints);
% get the row and columns (x,y)
[xVals,yVals] = ind2sub(size(im),points);
for ii = 1:numel(points)
x = xVals(ii);
y = yVals(ii);
try
% create the crosses, try statement is here to prevent index out of bounds
% not necessarily the best practice but whatever, it is only for demonstration
im(x,y) = 1;
im(x+1,y) = 1;
im(x-1,y) = 1;
im(x,y+1) = 1;
im(x,y-1) = 1;
catch err
end
end
% display the randomly generated image
imshow(im)
% create a simple cross filter
filter = [0,1,0;1,1,1;0,1,0];
figure; imshow(filter)
% perform convolution of the random image with the cross template
result = conv2(im,filter,'same');
% get the number of white pixels in filter
filSum = sum(filter(:));
% look for all points in the convolution results that matched identically to the filter
matches = find(result == filSum);
%validate all points found
sort(matches(:)) == sort(points(:))
% get x and y coordinate matches
[xMatch,yMatch] = ind2sub(size(im),matches);
I would highly suggest looking at the conv2 documentation on MATLAB's website.

To make all peaks clearly visible in Matlab

I finally solved my problem here with lennon310.
I have a picture of thousands of thin peaks in Time-Frequency picture.
I cannot see all the same time in one picture.
Depending on the physical width of my time window, some windows appear and some come visible.
Pictures of my data which I plot by imagesc
All pictures are from the same data points T, F, B.
How can you plot all peaks at once in a picture in Matlab?
You need to resize the image using resampling to prevent the aliasing effect (that craigim described as unavoidable).
For example, the MATLAB imresize function can perform anti-aliasing. Don't use the "nearest" resize method, that's what you have now.
Extension to #BenVoigt's answer
My try
B = abs(B);
F1 = filter2(B,T); % you need a different filter kernel because resolution is lower
T = filter2(B,F);
F = F1;
image1 = imagesc(B);
display1 = imresize(image1, [600 600], 'bilinear');
imshow(T*t, F*fs, display1);
where are some problems.
I get again picture where the resolution is not enough
2nd Extension to BenVoigt's answer
My suggestion for one kernel filter is with convolution of relative random error
data(find(data ~= 0)) = sin(pi .* data(find(data ~= 0))) ./ (pi*data(find(data ~= 0)));
data(find(data == 0)) = 1; % removing lastly the discontinuity
data1 = data + 0.0000001 * mean(abs(data(:))) * randn(size(data));
data = conv(data, data1);
Is this what BenVoigt means by the kernel filter for distribution?
This gives results like
where the resolution is still a problem.
The central peaks tend to multiply easily if I resize the window.
I had old code active in the above picture but it did not change the result.
The above code is still not enough for the kernel filter of the display.
Probably, some filter functions has to be applied to the time and frequency axis separately still, something like:
F1 = filter2(B,T); % you need a different kernel filter because resolution is lower
T = filter2(B,F);
F = F1;
These filters mess up the values on the both axis.
I need to understand them better to fix this.
But first to understand if they are the right way to go.
The figure has be resized still.
The size of the data was 5001x1 double and those of F and T 13635x1 double.
So I think I should resize lastly after setting axis, labels and title by
imresize(image, [13635 13635], 'bilinear');
since the distirbution is bilinear.
3rd Extension to BenVoigt's answer
I plot the picture now by
imagesc([0 numel(T)], [0 numel(F)], B);
I have a big aliasing problem in my pictures.
Probably, something like this should be to manipulate the Time-Frequency Representation
T = filter2(B,t); % you need a different filter kernel because resolution is lower
F = filter2(B,fs);
4th extension to BenVoigt's answer and comment
I remove the filters and addition of random relative errors.
I set the size of T,F,B, and run
imagesc([0 numel(T)], [0 numel(F)], B, [0 numel(B)])
I get still with significant aliasing but different picture
This isn't being flippant, but I think the only way is to get a wider monitor with a higher pixel density. MATLAB and your video card can only show so many pixels on the screen, and must decide which to show and which to leave out. The data is still there, it just isn't getting displayed. Since you have a lot of very narrow lines, some of them are going to get skipped when decisions are made as to which pixels to light up. Changing the time window size changes which lines get aliased away and which ones get lucky enough to show up on the screen.
My suggestions are, in no particular order: convolute your lines with a Gaussian along the time axis to broaden them, thus increasing the likelihood that part of the peak will appear on the screen. Print them out on a 600 dpi printer and see if they appear. Make several plots, each zooming in on a separate time window.
3rd Extension to BenVoigt's answer
My attempt to tell imagesc how big to make the image, instead of letting to use the monitor size, so it won't away data.
imagesc(T*t, F*fs, B, [50 50]);
% imagesc(T*t, F*fs, B');
% This must be last
imresize(image, 'bilinear', [64 30],);
I am not sure where I should specify the amount of pixels in x-axis and y-axis, either in the command imagesc or imresize.
What is the correct way of resizing the image here?

Mathematica Image Average Optimization

this is my first time asking question in stackoverflow. I tried hard not to make any guideline violations, but let me know if there is any—please understand since it is my first time.
I have a code in Mathematica, and I think it is REALLY slow.
I have a stack of images—couple hundreds, and I was creating a simple algorithm in Mathematica to combine images into a single image by averaging pixel data across the entire stack.
For example, assume that I have a pixel at 520 x 23, and I want to get all the number(data) from every single images in the stack and average that table. I want to repeat this process for every single pixel in my image. Then, I will be able to reconstruct the data into an image using Image[]. For the testing, I used 1024 by 1024 image, but I want to use 8000 by 8000 eventually.
Here is my code:
idlist = Map[ImageData, ilist];
Table[Table[Map[Mean,Transpose[Table[idlist[[a,b,c]],{a,1,Length[idlist]}]]],{b,1,1024}], {c, 1, 1024}]
I found out that my nested Table was faster than using a single Table for some weird reasons.
1.How should I optimize this?
2.Is there a benefit using any parallel computing for this specific implementation?
I'm not sure if I'm following your coding, but to get the mean pixel value for a bunch of images, you could simply do the following (it's fast):
imDim = 100;
n = 80;(*number of images*)
(*generate random image list*)
ilist = Array[RandomImage[1, {imDim, imDim}, ColorSpace -> "RGB"] &, n];
(*take mean*)
Image#Mean[ImageData /# ilist]
For a large collection of random images it should converge to homogeneous gray (RGB[.5,.5,.5]). Let's test it:
imDim = 3;
n = 2000;(*number of images*)
(*generate random image list*)
ilist = Array[RandomImage[1, {imDim, imDim}, ColorSpace -> "RGB"] &, n];
(*take mean*)
Image#Mean[ImageData /# ilist]

Resources