2

I am new to cross-correlation. I am working on images which having many multiple repeating pattern. I want to find shift in the image. I have initial image and then I shifted that image a bit in x and y direction. For example you can take any repeating pattern image like :- https://depositphotos.com/vector-images/repeat-structure.html. When I am using register_translation for finding shift in image, it's giving me the shift but its wrong. The reason behind is that there are multiple repeating structure in input image.

I want something which can give me multiple peaks after correlation so manually I can correct some code and can find shift in two images correctly. Is there any way to correct the code so that I can get not one but multiple peaks after doing cross-correlation of two images?

code what i did

image = cv2.imread('/content/download.jpg',0)
shift = (0,0)
offset_image = fourier_shift(np.fft.fftn(image), shift)
offset_image = np.fft.ifftn(offset_image)
print("Known offset (y, x): {}".format(shift))
shift, error, diffphase = register_translation(image, offset_image)
fig = plt.figure(figsize=(8, 3))
ax1 = plt.subplot(1, 3, 1)
ax2 = plt.subplot(1, 3, 2, sharex=ax1, sharey=ax1)
ax3 = plt.subplot(1, 3, 3)
ax1.imshow(image, cmap='gray')
ax1.set_axis_off()
ax1.set_title('Reference image')
ax2.imshow(offset_image.real, cmap='gray')
ax2.set_axis_off()
ax2.set_title('Offset image')
image_product = np.fft.fft2(image) * np.fft.fft2(offset_image).conj()
cc_image = np.fft.fftshift(np.fft.ifft2(image_product))
x=cc_image[0].real
x.sort()
print(x)
ax3.imshow(cc_image.real)
ax3.set_axis_off()
ax3.set_title("Cross-correlation")
plt.show()

Here in code if we look at croos-correlation image we can see there are lot of peak if there are repeating pattern in the image. But register_translation gives a shift on base of single peak. But there are many other peaks also so how i can get all of them.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
goodwin
  • 49
  • 4
  • 2
    Welcome to SO. Please read [ask] and [mcve]. Then adapt your post accordingly to meet the stack standard. – jlandercy Nov 29 '21 at 06:08
  • Are you looking for something like https://docs.opencv.org/3.4/d4/dc6/tutorial_py_template_matching.html ? – mrtpk Nov 29 '21 at 06:10
  • @tpk No its template matching here we just do cross correlation and find the peak where it is maximum. Here we will get only one max peak. But when we use cross-correlation where we want to find shift in image there also we do same thing but functions return us maximum peak. But i want it to return multiple return because there can be multiple matching in image with slight lower peak also. This question is related to how to find shift in image. – goodwin Nov 29 '21 at 06:35
  • It might be helpful to include since example images. – NoDataDumpNoContribution Nov 29 '21 at 06:50
  • 1
    @Trilarion you can take any repeating structure image like https://depositphotos.com/vector-images/repeat-structure.html – goodwin Nov 29 '21 at 06:57
  • key is **local** maxima – Christoph Rackwitz Nov 29 '21 at 07:42
  • @ChristophRackwitz about which Key you are talking about?? – goodwin Nov 29 '21 at 08:15
  • ignore that word, focus on the rest – Christoph Rackwitz Nov 29 '21 at 08:58
  • @goodwin, can you share the code that you have done for the single peak? I think as christoph has pointed out, you should modify the code such that it returns local peaks. – mrtpk Nov 29 '21 at 10:09
  • @ChristophRackwitz yes, but how I can put Local Maxima after doing cross-correlation. How I will decide the value of it. what all factor need to be consider?? how? – goodwin Nov 29 '21 at 10:11
  • @tpk I have added the code. So python register_translation works on a single peak which is maximum but it gives wrong answer because there can be another peaks also because there are lot of repeating structure. How can i get all the peaks in different places . – goodwin Nov 29 '21 at 10:22
  • 1
    https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_peak_local_max.html https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_extrema.html please show some initiative – Christoph Rackwitz Nov 29 '21 at 10:25
  • 1
    and point out if you've asked the same question before https://stackoverflow.com/questions/69763670/how-to-find-shift-in-image-using-cross-correlation-in-repeating-structure-image – Christoph Rackwitz Nov 29 '21 at 10:26
  • See non_max_suppression at https://www.pyimagesearch.com/2021/03/29/multi-template-matching-with-opencv/ or use masking for peak suppression at https://stackoverflow.com/questions/67368951/opencv-matchtemplate-and-np-where-keep-only-unique-values/67374288#67374288 – fmw42 Nov 29 '21 at 16:11
  • my favorite NMS on raster results is to grayscale-dilate and then compare for equality. not perfect because it doesn't suppress _equal_ maxima, but a lot more elegant than messing around with bboxes before it's necessary. orders of magnitude more sensible than erasing the global maximum and re-scanning. – Christoph Rackwitz Nov 29 '21 at 22:46

0 Answers0