Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 821
Corwin Perren Avatar asked Apr 13 '15 21:04

Corwin Perren


1 Answers

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
like image 64
meCHAoT Avatar answered Sep 28 '22 19:09

meCHAoT