繁体   English   中英

将C ++的OpenCV代码片段转换为Python

[英]Converting OpenCV code snippet from C++ to Python

我正在尝试将此代码转换为python。
谁能帮我?

    cv::Mat image;
while (image.empty()) 
{
    image = cv::imread("capture.jpg",1);
}
cv::imwrite("result.jpg",image);
`

在Python中,C ++的Mat变为一个numpy数组,因此图像操作变得与访问多维数组一样简单。 但是,方法名称在C ++和Python中都是相同的。

import cv2 #importing opencv module

img = cv2.imread("capture.jpg", 1) #Reading the whole image

cv2.imwrite("result.jpg", img) # Creating a new image and copying the contents of img to it

编辑 :如果要在生成图像文件后立即写入内容,则可以使用os.path.isfile() ,它根据给定目录中文件的存在返回bool值。

import cv2 
import os.path

while not os.path.isfile("capture.jpg"):
    #ignore if no such file is present.
    pass

img = cv2.imread("capture.jpg", 0)

cv2.imwrite("result.jpg", img)

您还可以参考docs了解每种方法和基本图像操作的详细实现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM