3

I'm new to python and open cv. I'm trying to find out how to load an image in opencv with python. Can any one provide an example (with code) explaining how to load the image and display it?

import sys
import cv
from opencv.cv import *
from opencv.highgui import *
ll="/home/pavan/Desktop/iff pics/out0291.tif"
img= cvLoadImage( ll );
cvNamedWindow( “Example1”, CV_WINDOW_AUTOSIZE );
cvShowImage( “Example1”, img );
cvWaitKey(10);
cvDestroyWindow( “Example");

3 Answers 3

7

There have been quite a few changes in the openCV2 API:

import cv
ll = "/home/pavan/Desktop/iff pics/out0291.tif"
img = cv.LoadImage(ll)
cv.NamedWindow("Example", cv.CV_WINDOW_AUTOSIZE )
cv.ShowImage("Example", img )
cv.WaitKey(10000)
cv.DestroyWindow("Example")

It is a simpler, quite cleaner syntax!

Also, you don't need trailing ; à-la-matlab. Last, be careful about the quotes you use.

For the newer openCV3 API, you should see the other answer to this question.

Sign up to request clarification or add additional context in comments.

Comments

0
import cv2
image_path = "/home/jay/Desktop/earth.jpg"
img = cv2.imread(image_path)  # For Reading The Image
cv2.imshow('image', img)      # For Showing The Image in a window with first parameter as it's title
cv2.waitKey(0)   #waits for a key to be pressed on a window 
cv2.destroyAllWindows() # destroys the window when the key is pressed

2 Comments

Please add a comment. Code-only answers leave out context for the readers.
possible link to openCV3 / python doc for imread : docs.opencv.org/3.0-beta/doc/py_tutorials/py_tutorials.html
0

There are 2 possible approaches to this:

  1. Using argparse (recommended):

    import cv2 import argparse ap = argparse.ArgumentParser() 

    ap.add_argument("-i", "--image", required = True,help = "Path to the image") args = vars(ap.parse_args()) 

    image = cv2.imread(args["image"])

This will take the image as an argument, will then convert the argument, add it to ap and the load it using the imread function()

To run it.

Go to your required folder

source activate your environment

python filename.py -i img.jpg

  1. Hardcoding the image location:

    import cv2 img = cv2.imread("\File\Loca\img.jpg") cv2.imshow("ImageName",img) cv2.waitKey(0) cv2.destroyAllWindows()

Run this similarly, omitting the arguments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.