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

时间:2015-05-22 14:44:54

标签: python c++

我正在尝试将此代码转换为python 有谁可以帮助我?

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

1 个答案:

答案 0 :(得分:1)

在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了解每种方法和基本图像操作的详细实现。

相关问题