Ask Your Question
0

OpenCV image display

asked 2020-09-16 11:36:28 -0500

updated 2020-09-20 10:18:36 -0500

supra56 gravatar image

Hello World!

I have been very interested in OpenCV for a while now, but never bothered getting it. I ended up downloading it through pip, making sure I had numpy, matplotlib, and the cv2 modules working. I tried running a very simple program:

from __future__ import print_function

import cv2 as cv

src1 = cv.imread(cv.samples.findFile('LinuxLogo.jpg'))
src2 = cv.imread(cv.samples.findFile('WindowsLogo.jpg'))

however I get this error:

Traceback (most recent call last):  
File
"C:/Users/•••••••••/Documents/Python_Programs/adding_images.py",
line 5, in <module>
src1 = cv.imread(cv.samples.findFile('LinuxLogo.jpg'))
cv2.error: OpenCV(4.4.0)
C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-m5rt9vz0\opencv\modules\core\src\utils\samples.cpp:62:
error: (-2:Unspecified error) OpenCV
 samples: Can't find required data
 file: LinuxLogo.jpg in function
 'cv::samples::findFile'

I think what may be happening here is that this module was set up on the user appveyor and it doesn't work on my computer? Or, (probably more likely) something completely different. Either way, any help is greatly appreciated! (Note:Open CV version is up to date, python 3 is used)

edit retag flag offensive close merge delete

Comments

1

I guess you should forget samples.findFile and just call imread with your own path to any file you wish to open, unless you have a specific need to open that logo, that way...

mvuori gravatar imagemvuori ( 2020-09-16 12:36:10 -0500 )edit
sturkmen gravatar imagesturkmen ( 2020-09-16 12:44:49 -0500 )edit

mvuroi what would be the syntax to do that?

pythonCoder gravatar imagepythonCoder ( 2020-09-16 13:05:42 -0500 )edit

I saw the documentation about the .findFile part but not sure how to use it.

pythonCoder gravatar imagepythonCoder ( 2020-09-16 13:10:39 -0500 )edit

Both @berak, @mvuori and @sturkmen are pointing wrong

supra56 gravatar imagesupra56 ( 2020-09-20 10:43:40 -0500 )edit

2 answers

Sort by » oldest newest most voted
0

answered 2020-09-20 03:19:11 -0500

berak gravatar image

I ended up downloading it through pip

that's why you don't have a samples or even samples/data folder, so the helper functions from cv2.samples cannot find anything. just remove the cv2.samples.findFile() part from your code, and manage your images manually.

edit flag offensive delete link more
-1

answered 2020-09-20 10:06:16 -0500

supra56 gravatar image

updated 2020-09-20 10:06:56 -0500

The cv2.samples can find any folders and including currently folder. I noticed that you've forgotten to downloading images. That why you're getting an ERROR: OpenCV samples: Can't find required data file: LinuxLogo.jpg in function 'cv::samples::findFile' or some typo error such as misspelled filename. Here is link for images: LinuxLogo.jpg and WindowsLogo.jpg Warning. Since we are adding src1 and src2, they both have to be of the same size (width and height) and type. Here is code that run faster:

#!/usr/bin/python3.7.3
#OpenCV 4.4.0, Raspberry pi 3/3b/4/, Buster ver10
#Date: 20th September, 2020

from __future__ import print_function
import cv2 as cv

alpha = 0.5

src1 = cv.imread(cv.samples.findFile('LinuxLogo.jpg'))
src2 = cv.imread(cv.samples.findFile('WindowsLogo.jpg'))

beta = (1.0 - alpha)
dst = cv.addWeighted(src1, alpha, src2, beta, 0.0)
cv.imwrite('logo.jpg', dst)

cv.imshow('dst', dst)
cv.waitKey(0)
# [display]
cv.destroyAllWindows()

Output image alpha = 0.5:

image description

Output image alpha = 0.0.1-0:

image description

edit flag offensive delete link more

Comments

Downvote reason: no need to repeate documented features here

sturkmen gravatar imagesturkmen ( 2020-09-20 10:41:46 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-09-16 11:36:28 -0500

Seen: 102 times

Last updated: 2 days ago