【问题标题】:How to do unit testing on Image as input and output in python?如何对图像进行单元测试作为python中的输入和输出?
【发布时间】:2019-06-02 14:41:38
【问题描述】:

我正在 Python 中对对象检测code(已接受的答案)进行单元测试。我知道在单元测试中,我们基本上将测试参数放入我们在程序中定义的函数中,然后输入预期的结果。如果输出了预期的结果,就OK了,否则就报错了。

所以我的问题是我的 inputImage 而我的 output 也是一个图像(即 在图像),然后,使用条形图和带有滑块的直方图表示结果。 如何对此类数据进行单元测试?

到目前为止,这是我尝试过的(这个code保存为cirCode)

from unittest import TestCase

import unittest
from unittest import TestCase
import cirCode

class TestFind_circles(TestCase):

        def setUp(self):
            pass

        def tearDown(self):
            pass

        #def test_circle(self):
         #   self.fail()

        def test_find_circles(self):

            Negative_circles, Positive_circles, out_filepath, circles, threshold = cirCode.find_circles('blobs.jpg')

            self.assertEqual(Negative_circles, 20)
            self.assertEqual(Positive_circles, 8)

if __name__ == '__main__':
    unittest.main()

现在,我不知道如何测试def circle功能。另外,我不确定这是否是测试 find_circles 功能的正确方法。

你们有什么更好的想法来对此code 进行单元测试,以及如何继续对 circle 函数进行单元测试?

【问题讨论】:

  • 它返回位图吗?已知输入是否相同?如果是这样,您至少可以对返回的位图与预期进行二进制比较。
  • @MarcinOrlowski 它从 Otsu 方法返回阈值,提供有关圆心坐标以及半径和平均 RGB 值的信息的圆,输出文件名和负数圈和正圈。所以,不,它不返回位图。但是在大图像上进行二进制比较会很困难,不是吗?

标签: python unit-testing opencv oop hough-transform


【解决方案1】:

不确定你在问什么,我假设你是在要求 opencv 函数和变量的单元测试方法。

下面的opencv python测试样本怎么样?他们做了你正在尝试做的类似工作。他们有几十个样本测试用例。

https://github.com/opencv/opencv/tree/master/modules/python/test

例如

import cv2 as cv
import numpy as np
import sys
from numpy import pi, sin, cos

from tests_common import NewOpenCVTests

def circleApproximation(circle):

    nPoints = 30
    dPhi = 2*pi / nPoints
    contour = []
    for i in range(nPoints):
        contour.append(([circle[0] + circle[2]*cos(i*dPhi),
            circle[1] + circle[2]*sin(i*dPhi)]))

    return np.array(contour).astype(int)

【讨论】:

  • 我想对代码中的函数进行单元测试(链接)。我在问题“......如何测试 def circle 函数。另外,我不确定这是否是测试 find_circles 函数的正确方法。”。感谢您的链接。我一定会检查出来的:)。我希望它能让我更好地了解如何继续前进。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
  • 2016-06-02
  • 2016-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多