-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Open
Description
System information (version)
- OpenCV => 4.3.0
- Operating System / Platform => Windows 10, 64 Bit, Version 2004 (OS Build 19041.572)
- Compiler => python 3.7.6 | packaged by conda-forge | (default, Jun 1 2020, 18:11:50) [MSC v.1916 64 bit (AMD64)]
Detailed description
I wanted to unwrap an image like in this C++ tutorial (https://docs.opencv.org/4.5.0/d9/dfb/tutorial_unwrap.html) and wanted to use python for this.
It is not possible to use the histogram unwrapping algorithm of opencv in python. In Pycharm, I got the error: "Process finished with exit code -1073741819 (0xC0000005)" after executing the function cv2.phase_unwrapping_HistogramPhaseUnwrapping.unwrapPhaseMap.
I used pip install opencv-contrib-python to get opencv for python.
Steps to reproduce
import numpy as np
import cv2
import sys
print('python ' + sys.version)
print('numpy version: ' + np.__version__)
print('cv2 version: ' + cv2.__version__)
add_shadow_mask = False
X, Y = np.meshgrid(np.linspace(-1, 1, 512) * 5, np.linspace(-1, 1, 512) * 5)
img = -(X * X + Y * Y)
print('Image size: %dx%d pixels', img.shape[0], img.shape[1])
# add noise
img = img + np.random.rand(img.shape[0], img.shape[1]) * 0.5
# add an ignored area for shadow-mask
if add_shadow_mask:
shadow_mask = np.zeros_like(img)
shadow_mask[int(shadow_mask.shape[0] / 4): 3 * int(shadow_mask.shape[0] / 4),
int(shadow_mask.shape[1] / 4): 3 * int(shadow_mask.shape[1] / 4)] = 1
img[np.where(shadow_mask)] = 0
# wrap the image
img = np.mod(img, 2 * np.pi)
img[(img == 0) & (img > 0)] = 2 * np.pi
# Make sure it is float32
img = img.astype(np.float32)
# show image as uint8
cv2.imshow('wrapped image', ((img+np.pi)/(2*np.pi)*255).astype(np.uint8))
cv2.waitKey(1)
cv2_unwrap_img = np.copy(img)
# Phase unwrapping with OpenCV #
# create params
params = cv2.phase_unwrapping_HistogramPhaseUnwrapping_Params()
params.height = img.shape[0]
params.width = img.shape[1]
print('params.height: %i' % params.height)
print('params.width: %i' % params.width)
print('params.histThresh: %f' % params.histThresh)
print('params.nbrOfLargeBins: %i' % params.nbrOfLargeBins)
print('params.nbrOfSmallBins: %i' % params.nbrOfSmallBins)
# create histogram unwrapping instance
unwrapping_instance = cv2.phase_unwrapping_HistogramPhaseUnwrapping()
unwrapping_instance.create(params)
# unwrap
if not add_shadow_mask:
unwrapping_instance.unwrapPhaseMap(img, cv2_unwrap_img, shadowMask=None)
else:
unwrapping_instance.unwrapPhaseMap(img, cv2_unwrap_img, shadowMask=shadow_mask)
# show image
cv2.imshow('wrapped image', ((cv2_unwrap_img+np.pi)/(2*np.pi)*255).astype(np.uint8))
cv2.waitKey(1)