You want to detect some object (image B) in image A.
Therefore you can use the histogram, that is the color/grayscale distribution of the object’s image. Shorthand said, you have a look onto which values are characteristic for the object, which means, they are more probable to appear in the object’s image than in the rest of the image. Then you select the region in Image A which has most of these characteristic pixel values.
This is done the following way.
Calculate both histograms for image A and B.
By dividing the histogram of image B by the histogram of image A element wise, you get high values for those bins, that have a relative high count for image B in comparison to image A. Note that values, that do not appear in image A lead to infinity, but later on, these bins wont be used. But be careful, if you do more operations on the histogram, like smoothing.
The new “histogram” now contains something like likelihood values telling how probable an intensity value belongs to the object.
Now we map each pixel of image A to its likelihood and take the average over a region with size of image B.
High values in the modified image indicate a high probability that this region contains the object.
Here some Octave code:
function [idy, idx] = histMatch(A, B) %HISTMATCH find Pattern B in A using Histogram % hit: how to read images into matrices %A = imread("img1.png"); %B = imread("imgobject.png"); %calculate histograms hA = histc(A(:), [0:1:255]); hB = histc(B(:), [0:1:255]); %calculate division (max 1) hC = hB./hA; %set maximum 1 (not needed, are not accessed anyway) hC = min(hC, 1); %map values from A to hC A2 = hC(A); %create mask and convolute mask = ones(size(B)); A3 = conv2(A2, mask, "same"); %determine coordinates of maximum %maximum per column with rowIndex [colMax, rowIndices] = max(A3); %maximum over the maxima [max, colIndex] = max(colMax); idx = colIndex; idy = rowIndices(idx); end