18

After spending hours trying out others' suggestions, I still can't get OpenCV to work. I'd like to build a Python script that checks an image's/PDF's color at a certain area (it's for a printing company to verify that documents have a 0.5mm white border, as this is their machine's preferred format). That said, I'm planning on using OpenCV's color detection capabilities to set an RGB tolerance for a document's contours.

I've tried installing OpenCV with brew, brew install homebrew/science/, sudo pip, sudo pip3, pip and pip3, but I keep getting the following error:

ModuleNotFoundError: No module named 'cv2'

What confuses me most is that it seems I've successfully installed OpenCV when I enter pkg-config opencv --cflags in terminal:

-I/usr/local/Cellar/opencv/3.3.1_1/include/opencv -I/usr/local/Cellar/opencv/3.3.1_1/include

Is the wrapper no longer supporter for Python 3.6? If so, where could I get a similar package?

Here's what I'm working with so far:

import cv2
import numpy as np

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

contours,_ = cv2.findContours(img, cv2.RETR_LIST, cv2.cv.CV_CHAIN_APPROX_NONE)

lst_intensites = [(255, 255, 255)]

for i in range(len(contours)):
    cimg = np.zeros_like(img)
    cv2.drawContours(cimg, contours, i, color=255, thickness=-1)

    pts = np.where(cimg == 255)
    lst_intensities.append(img[pts[0], pts[1]])
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
solo
  • 743
  • 2
  • 6
  • 17

4 Answers4

26

On Windows, you can install with pip:

pip install opencv-python
DingDong
  • 261
  • 3
  • 3
14

Well I was also facing the same issue today but I finally installed it in Anaconda and it's working fine:

conda install -c conda-forge opencv
conda install -c conda-forge/label/broken opencv 

source:Opencv Installation

Happy Coding:)

sonu_chauhan
  • 315
  • 4
  • 11
  • 1
    Thanks! Conda did the trick after some troubleshooting. Here's what ended up solving the problem: `conda install -c conda-forge nb_conda` – solo Nov 23 '17 at 12:54
  • I ran `conda install -c conda-forge opencv` once and it did not work. Then I did various things, and ran this command again, now it worked. For anyone else stumble upon this, I also had a ROS installation that will mess up the `sys.path` and I have to delete it from `sys.path` before being able to read the correct opencv. – yuqli Mar 24 '19 at 19:39
5

On Ubuntu, you can install the opencv depends like:

sudo apt-get install python-opencv 

or install it with pip ( pyhon package management tools ):

pip install opencv-python

Refer to similar questions OpenCV - cannot find module cv2.

Aryamaan Goswamy
  • 319
  • 1
  • 2
  • 16
張正軒
  • 109
  • 1
  • 3
0

For windows, this will work

pip install --upgrade pip setuptools wheel

then install opencv,

pip install opencv-python
Gowthaman
  • 137
  • 1
  • 10