如何阻止numpy hstack更改opencv中的像素值 [英] how to stop numpy hstack from changing pixel values in opencv
问题描述
我正在尝试使用opencv在python上显示图像,并在其上添加侧面板.当我使用np.hstack
时,主图像变成难以识别的白色,只有少量颜色.这是我的代码:
I'm trying to get an image to display in python using opencv, with a side pane on it. When I use np.hstack
the main picture becomes unrecognizably white with only a small amount of color. Here's my code:
img = cv2.imread(filename)
img_with_gt, gt_pane = Evaluator.return_annotated(img, annotations)
both = np.hstack((img_with_gt, gt_pane))
cv2.imshow("moo", both)
cv2.waitKey(0)
cv2.destroyAllWindows()
这是生成的图片
但是,如果我查看img_with_gt
,它看起来是正确的.
But If I view img_with_gt
it looks correct.
甚至可以与gt_pane
我似乎不知道为什么会这样.
I can't seem to figure out why this is happening.
推荐答案
我看到的唯一方法是,如果两个图像之间的数据类型不一致.确保在return_annotated
方法内部,img_with_gt
和gt_pane
都共享相同的数据类型.
The only way I can see that happening is if the data types between the two images don't agree. Make sure that inside your return_annotated
method, both img_with_gt
and gt_pane
both share the same data type.
您提到了以下事实,您正在为gt_pane
作为float64
分配空间.这表示[0-1]
范围内的强度/颜色.将图像转换为uint8
,然后将结果乘以255,以确保两个图像之间的兼容性.如果要保持图像不变,而要处理分类图像(右图),请转换为float64
然后除以255.
You mentioned the fact that you're allocating space for the gt_pane
to be float64
. This represents intensities / colours within the span of [0-1]
. Convert the image to uint8
and multiply the result by 255 to ensure compatibility between the two images. If you want to leave the image untouched and work on the classification image (the right one), convert to float64
then divide by 255.
但是,如果您希望保持该方法不变,可以进行以下简单修复:
However, if you want to leave the method untouched, a simple fix could can be:
both = np.hstack(((255*img_with_gt).astype(np.uint8), gt_pane))
您也可以采用其他方法:
You can also go the other way around:
both = np.hstack((img_with_gt, gt_pane.astype(np.float64)/255.0))
这篇关于如何阻止numpy hstack更改opencv中的像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!