Fix Python – OpenCV giving wrong color to colored images on loading

I’m loading in a color image in Python OpenCV and plotting the same. However, the image I get has it’s colors all mixed up.
Here is the code:
import cv2
import numpy as np
from numpy import array, arange, uint8
from matplotlib import pyplot as plt

img = cv2.imread(‘lena_caption.png’, cv2.IMREAD_COLOR)
bw_img = cv2.cvtColor(img, cv2.COLOR_BGR2G….

Fix Python – ImportError: DLL load failed: %1 is not a valid Win32 application. But the DLL’s are there

I have a situation very much like the one at Error “ImportError: DLL load failed: %1 is not a valid Win32 application”, but the answer there isn’t working for me.
My Python code says:
import cv2

But that line throws the error shown in the title of this question.
I have OpenCV installed in C:\lib\opencv on this 64-bit machine. I’m using 64-bit Pyt….

Fix Python – ImportError: cannot open shared object file: No such file or directory

When trying to import OpenCV, using import cv2 I get the following error:
/usr/local/lib/python2.7/dist-packages/cv2/ in ()
8 # make IDE’s (PyCharm) autocompletion happy
—-> 9 from .cv2 import *
11 # wildcard import above does not import “private” variables like __version__


Fix Python – Convert image from PIL to openCV format

I’m trying to convert image from PIL to OpenCV format. I’m using OpenCV 2.4.3.
here is what I’ve attempted till now.
>>> from PIL import Image
>>> import cv2 as cv
>>> pimg =‘D:\\traffic.jpg’) #PIL Image
>>> cimg =,cv.IPL_DEPTH_8U,3) #CV Image

Fix Python – OpenCV – Depth map from Uncalibrated Stereo System

I’m trying to get a depth map with an uncalibrated method.
I can obtain the fundamental matrix by finding correspondent points with SIFT and then using cv2.findFundamentalMat. I then use cv2.stereoRectifyUncalibrated to get the homography matrices for each image. Finally I use cv2.warpPerspective to rectify and compute the disparity, but this does….

Fix Python – How to remove convexity defects in a Sudoku square?

I was doing a fun project: Solving a Sudoku from an input image using OpenCV (as in Google goggles etc). And I have completed the task, but at the end I found a little problem for which I came here.
I did the programming using Python API of OpenCV 2.3.1.
Below is what I did :

Read the image
Find the contours
Select the one with maximum area, ( a….

Fix Python – How to crop an image in OpenCV using Python

How can I crop images, like I’ve done before in PIL, using OpenCV.
Working example on PIL
im =‘0.png’).convert(‘L’)
im = im.crop((1, 1, 98, 33))‘_0.png’)

But how I can do it on OpenCV?
This is what I tried:
im = cv.imread(‘0.png’, cv.CV_LOAD_IMAGE_GRAYSCALE)
(thresh, im_bw) = cv.threshold(im, 128, 255, cv.THRESH_OTSU)
im = ….

Fix Python – How to detect a Christmas Tree?

Which image processing techniques could be used to implement an application that detects the Christmas trees displayed in the following images?

I’m searching for solutions that are going to work on all these images. Therefore, approaches that require training haar cascade classifiers or template matching are not very interesting.
I’m looking….