Python 2.7 and Opencv 3.0.0 cvtColor not working for BGR/RGB conversions
Asked Answered
D

1

6

So I realize I may be asking a lot out of a non-finished version of opencv, but I'm having strange issues with the cvtColor method that I can't find any other references to people having elsewhere. First, as some prefacing, I'm writing a multithreaded gui application using PyQt4, QThreads, Python 2.7, and opencv on the beaglebone black. My current source can be found on github HERE. I was originally using the debian repo version of opencv but it turned out to be so outdated that it didn't have some of the features I was looking for, namely the simpleblobdetector class, and was extremely slow. With that in mind, I compiled the latest opencv 3.0.0 from scratch and ever since then it's been acting strange. I eventually narrowed it down to an issue with cvtColor. I then simplified it to bare minimum code to make sure it wasn't something else causing the issue. Here's what I have that I've been using for testing.

import cv2

img = cv2.imread('images/original_image.png', cv2.IMREAD_COLOR)

rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
bgr_img = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2BGR)

cv2.imwrite("images/after_convert_to_rgb.png", rgb_img)
cv2.imwrite("images/after_convert_to_gray.png", gray_img)
cv2.imwrite("images/after_convert_back_to_bgr.png", bgr_img)

The resulting images from this code can be found HERE.

Needless to say, I'm stumped at this point. I find it particularly weird that the conversion to gray works perfectly while the other two don't work at all. I've had a couple of my friends who work with opencv check both my original code and this test code and can't see anything wrong with it. Also, though not as part of this test code, I did try reading in different file formats and images from a variety of sources. It also does the same thing manipulating an opencv frame received from the VideoCapture class as that's where I first saw the issue and what I'm ultimately trying to do.

So has anyone witnessed something like this before in opencv 3.0? Am I better off just custom compiling opencv 2.4 and using that instead? I would have done that in the first place, but I was following guides on custom compiling opencv for the beaglebone black in particular and they all used the latest 3.0 so I figured it would be fine. Anyway, figured it'd be worth checking before I do the compilation process again as it's tended to take me a couple of days to get it right with it doing so overnight.

EDIT: Just in case anyone else goes looking and wants to know what I found out. It is definitely a bug in the release candidate of opencv 3.0 that I downloaded. I wasn't able to find a fix for that version and ultimately had to downgrade to version 2.4.10. Since the downgrade, everything is now working fine.

Deil answered 13/4, 2015 at 21:24 Comment(3)
Looks like a bug. I think it will be better to submit bug report to OpenCV issue tracker : code.opencv.orgPriority
Did this turn out to be a bug?Ogrady
Yes, it turned out to be a bug that I could not find any answers to so I ended up recompiling 2.4.10 and using that instead.Deil
S
3

Though it is not a "OpenCV-solution" you could just rearrange the color channels using pure python as the cv2-Interface is using numpy-arrays for data-storage:

rgb_img = bgr_img[:,:,::-1]    #bgr --> rgb
bgr_img = rgb_img[:,:,::-1]    #bgr --> rgb
Soggy answered 7/12, 2015 at 15:13 Comment(1)
This does not work. My red still comes out as blue.Stopple

© 2022 - 2024 — McMap. All rights reserved.