I have a code that prints float32 for an image that I am processing on my local machine. However, the code does not work when we port it to another machine and it somehow prints the same image as float64. Why is that so? Is it because of the machine architecture, or OpenCV versions, or the numpy version?
Local machine:
print(image.dtype) : float32
Remote machine:
print(image.dtype) : float64
As a result of this, obviously, none of the image processing functions work in the code. What is the best way to overcome this?
EDIT: OpenCV version 4.5.5
OS: Ubuntu 20.04
Obviously, both machines run the same OS and have the same OpenCV versions.
Code:
normalized = np.ones_like(frame,dtype=np.float32)
edges = filters.sobel(normalized) # here it becomes float64
edges = img_as_float32(edges) # required in some systems so that system does not throw float64/float32 in the next line
gray = cv2.cvtColor(edges, cv2.COLOR_BGR2GRAY)
more info please. what you’ve given so far is far from sufficient to even hypothesize.
you talk of architectures yet there’s no info on that at all. do you want us to think both machines run the same ubuntu and the same opencv version? what architectures are involved? who built OpenCV?
OK, you were right. The reason why the code worked after I recompiled OpenCV was that the same setup file also re-installed Skimage, which was the actual problem.
normalized = np.ones_like(frame,dtype=np.float32)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
for i in range(3):
normalized[:,:,i] = np.take(cumsum[i,:], frame[:,:,i]) / hist[i,:].sum()
print("normalized:", normalized.dtype)
edges = filters.sobel(normalized)
print("edges:", edges.dtype)
gray = cv2.cvtColor(edges, cv2.COLOR_BGR2GRAY)
output:
normalized: float32
edges: float64
So, it appears the function edges = filters.sobel(normalized) from Skimage caused the problem.
Those who faced this problem, although this is OpenCV forum and not Skimage one, here is how to solve it:
from skimage.util import img_as_float32
image = img_as_float32(any_opencv_image)
I also apologize on my own behalf. I removed the misleading posts, and I updated my original question so that it is a complete question now.