由于PIL与cv2加载图像不同,我经常处理rgb-> bgr问题。通常我可以做到
orig = orig [...,:: - 1]
但是我遇到了一个奇怪的打嗝(我正在使用cv2 3.2);稍后用于向图像添加一些框的功能
TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)
除了显示的工作之外的所有方法的各种方法,如图所示。 rgb-> bgr之后的图像看起来很好,具有正确的暗淡。深度复制是由于偏执,该问题是由image_np的一些变化引起的。
image_np = load_image_into_numpy_array(image)
bgr_img = copy.deepcopy(image_np)
print('origshape {}'.format(bgr_img.shape))
#bgr_img = bgr_img[...,::-1] #fails
#bgr_img = bgr_img[...,[2,1,0]]) #fails
#bgr_img = np.array(bgr_img[...,[2,1,0]]) #fails
bgr_img = cv2.cvtColor(bgr_img,cv2.COLOR_RGB2BGR) #works
cv2.imshow('bgr',bgr_img)
cv2.waitKey(0)
print('finalshape {}'.format(bgr_img.shape))
...
失败的功能是
def bb_with_text(img_arr,bb_xywh,text,boxcolor=[50,255,50]):
text_color=[0,50,255]
text_bgnd_color=[220,255,180]
cv2.rectangle(img_arr,(bb_xywh[0],bb_xywh[1]),(bb_xywh[0]+bb_xywh[2],bb_xywh[1]+bb_xywh[3]),color=boxcolor,thickness=2)
img_arr[bb_xywh[1]:bb_xywh[1]+20,bb_xywh[0]:bb_xywh[0]+ bb_xywh[2]]=img_arr[bb_xywh[1]:bb_xywh[1]+20,bb_xywh[0]:bb_xywh[0]+bb_xywh[2]]/2)+np.array(text_bgnd_color)/2
cv2.putText(img_arr,text,(bb_xywh[0]+5,bb_xywh[1]+20),cv2.FONT_HERSHEY_PLAIN, 1, text_color)
return img_arr
带
-> 2063 cv2.rectangle(img_arr,(bb_xywh[0],bb_xywh[1]),(bb_xywh[0]+bb_xywh[2],bb_xywh[1]+bb_xywh[3]),color=boxcolor,thickness=2)
2064 img_arr[bb_xywh[1]:bb_xywh[1]+20,bb_xywh[0]:bb_xywh[0]+bb_xywh[2]]=(img_arr[bb_xywh[1]:bb_xywh[1]+20,bb_xywh[0]:bb_xywh[0]+bb_xywh[2]]/2)+np.array(text_bgnd_color)/2
2065 cv2.putText(img_arr,text,(bb_xywh[0]+5,bb_xywh[1]+20),cv2.FONT_HERSHEY_PLAIN, 1, text_color)
TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)
它不是一个showtopper,因为cv2转换可以解决这个问题但是它非常令人不安