我想使用OpenCV从我的网络摄像头捕获并保存大量图像.这是我目前的代码:
import cv2
camera = cv2.VideoCapture(0)
for i in range(10):
return_value, image = camera.read()
cv2.imwrite('opencv'+str(i)+'.png', image)
del(camera)
Run Code Online (Sandbox Code Playgroud)
这个问题是我不知道什么时候拍摄这些照片,所以很多照片都会模糊不清.我的问题是:有没有办法在点击键盘键上拍摄图像?
还有更好的方法来拍摄多个图像,而不是范围?
我有以下代码:
total_frames = 50
cv2.cv.NamedWindow("Dragonfly Simulation")
cv2.cv.StartWindowThread()
for i in range(total_frames):
# do stuff
img_name = # something
img = cv2.cv.LoadImage(img_name)
cv2.cv.ShowImage("Dragonfly Simulation", img)
cv2.cv.WaitKey(2)
cv2.cv.DestroyWindow("Dragonfly Simulation")
cv2.cv.WaitKey(1)
# rest of code
Run Code Online (Sandbox Code Playgroud)
那么它做了什么:
但是在这种情况下,我有total_frame之前给出的.我不希望这样.
相反,我想要一个执行以下操作的代码:
但是,我在OpenCV中找不到可以检测用户何时关闭窗口的函数.有人可以建议一个解决方法吗?
OpenCV 2.2版,C++接口.
在带有以下代码段的窗口中显示加载的图像时
cvStartWindowThread();
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image );
while( 1 ) {
if( cvWaitKey(100) == 27 ) break;
}
Run Code Online (Sandbox Code Playgroud)
通过close button用鼠标按下而不是使用转义键来关闭图像时遇到问题.
在这种情况下,我的程序将被阻止,while退出它的唯一方法是停止执行,这显然是不希望的.
是否有任何功能可以控制是否close button按下了?这样我就可以在while循环中添加它:
例如
while( 1 ) { …Run Code Online (Sandbox Code Playgroud)