Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I want to use OpenCV2.0 and Python2.6 to show resized images. I used and adopted the example at 

http://opencv.willowgarage.com/documentation/python/cookbook.html 

but unfortunately, this code is for OpenCV2.1 and seems not to be working on 2.0. Here my code:

import os, glob

import cv

ulpath = "exampleshq/"

for infile in glob.glob( os.path.join(ulpath, "*.jpg") ):

    im = cv.LoadImage(infile)

    thumbnail = cv.CreateMat(im.rows/10, im.cols/10, cv.CV_8UC3)

    cv.Resize(im, thumbnail)

    cv.NamedWindow(infile)

    cv.ShowImage(infile, thumbnail)

    cv.WaitKey(0)

    cv.DestroyWindow(name)

Since I cannot use

cv.LoadImageM

I used

cv.LoadImage

instead, which was no problem in other applications. Nevertheless, cv.iplimage has no attribute rows, cols or size. Can anyone give me a hint, how to solve this problem? Thanks.

1 Answer

0 votes
by (106k points)

You can use the below-mentioned code to resize the image:-

import cv2 

img = cv2.imread('YOUR_PATH_TO_IMG') 

height, width = img.shape[:2] 

max_height = 300 

max_width = 300 

if max_height < height or max_width < width: 

scaling_factor = max_height / float(height) 

if max_width/float(width) < scaling_factor: 

scaling_factor = max_width / float(width)

img = cv2.resize(img, None, fx=scaling_factor, 

fy=scaling_factor, interpolation=cv2.INTER_AREA)

cv2.imshow("Shrinked image", img) 

key = cv2.waitKey()

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...