chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1 @@
|
||||
misc/java/src/cpp/objdetect_converters.hpp
|
||||
@@ -0,0 +1,69 @@
|
||||
{
|
||||
"ManualFuncs" : {
|
||||
"QRCodeEncoder" : {
|
||||
"QRCodeEncoder" : {
|
||||
"j_code" : [
|
||||
"\n",
|
||||
"/** Generates QR code from input string.",
|
||||
"@param encoded_info Input bytes to encode.",
|
||||
"@param qrcode Generated QR code.",
|
||||
"*/",
|
||||
"public void encode(byte[] encoded_info, Mat qrcode) {",
|
||||
" encode_1(nativeObj, encoded_info, qrcode.nativeObj);",
|
||||
"}",
|
||||
"\n"
|
||||
],
|
||||
"jn_code": [
|
||||
"\n",
|
||||
"private static native void encode_1(long nativeObj, byte[] encoded_info, long qrcode_nativeObj);",
|
||||
"\n"
|
||||
],
|
||||
"cpp_code": [
|
||||
"//",
|
||||
"// void cv::QRCodeEncoder::encode(String encoded_info, Mat& qrcode)",
|
||||
"//",
|
||||
"\n",
|
||||
"JNIEXPORT void JNICALL Java_org_opencv_objdetect_QRCodeEncoder_encode_11 (JNIEnv*, jclass, jlong, jbyteArray, jlong);",
|
||||
"\n",
|
||||
"JNIEXPORT void JNICALL Java_org_opencv_objdetect_QRCodeEncoder_encode_11",
|
||||
"(JNIEnv* env, jclass , jlong self, jbyteArray encoded_info, jlong qrcode_nativeObj)",
|
||||
"{",
|
||||
"",
|
||||
" static const char method_name[] = \"objdetect::encode_11()\";",
|
||||
" try {",
|
||||
" LOGD(\"%s\", method_name);",
|
||||
" Ptr<cv::QRCodeEncoder>* me = (Ptr<cv::QRCodeEncoder>*) self; //TODO: check for NULL",
|
||||
" const char* n_encoded_info = reinterpret_cast<char*>(env->GetByteArrayElements(encoded_info, NULL));",
|
||||
" const jsize n_encoded_info_size = env->GetArrayLength(encoded_info);",
|
||||
" Mat& qrcode = *((Mat*)qrcode_nativeObj);",
|
||||
" (*me)->encode( std::string(n_encoded_info, n_encoded_info_size), qrcode );",
|
||||
" } catch(const std::exception &e) {",
|
||||
" throwJavaException(env, &e, method_name);",
|
||||
" } catch (...) {",
|
||||
" throwJavaException(env, 0, method_name);",
|
||||
" }",
|
||||
"}",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"type_dict": {
|
||||
"NativeByteArray": {
|
||||
"j_type" : "byte[]",
|
||||
"jn_type": "byte[]",
|
||||
"jni_type": "jbyteArray",
|
||||
"jni_name": "n_%(n)s",
|
||||
"jni_var": "jbyteArray n_%(n)s = env->NewByteArray(static_cast<jsize>(%(n)s.size())); env->SetByteArrayRegion(n_%(n)s, 0, static_cast<jsize>(%(n)s.size()), reinterpret_cast<const jbyte*>(%(n)s.c_str()));",
|
||||
"cast_from": "std::string"
|
||||
},
|
||||
"vector_NativeByteArray": {
|
||||
"j_type": "List<byte[]>",
|
||||
"jn_type": "List<byte[]>",
|
||||
"jni_type": "jobject",
|
||||
"jni_var": "std::vector< std::string > %(n)s",
|
||||
"suffix": "Ljava_util_List",
|
||||
"v_type": "vector_NativeByteArray"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "objdetect_converters.hpp"
|
||||
|
||||
#define LOG_TAG "org.opencv.objdetect"
|
||||
|
||||
void Copy_vector_NativeByteArray_to_List(JNIEnv* env, std::vector<std::string>& vs, jobject list)
|
||||
{
|
||||
static jclass juArrayList = ARRAYLIST(env);
|
||||
jmethodID m_clear = LIST_CLEAR(env, juArrayList);
|
||||
jmethodID m_add = LIST_ADD(env, juArrayList);
|
||||
|
||||
env->CallVoidMethod(list, m_clear);
|
||||
for (std::vector<std::string>::iterator it = vs.begin(); it != vs.end(); ++it)
|
||||
{
|
||||
jsize sz = static_cast<jsize>((*it).size());
|
||||
jbyteArray element = env->NewByteArray(sz);
|
||||
env->SetByteArrayRegion(element, 0, sz, reinterpret_cast<const jbyte*>((*it).c_str()));
|
||||
env->CallBooleanMethod(list, m_add, element);
|
||||
env->DeleteLocalRef(element);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html
|
||||
|
||||
#ifndef OBJDETECT_CONVERTERS_HPP
|
||||
#define OBJDETECT_CONVERTERS_HPP
|
||||
|
||||
#include <jni.h>
|
||||
#include "opencv_java.hpp"
|
||||
#include "opencv2/core.hpp"
|
||||
|
||||
void Copy_vector_NativeByteArray_to_List(JNIEnv* env, std::vector<std::string>& vs, jobject list);
|
||||
|
||||
#endif /* OBJDETECT_CONVERTERS_HPP */
|
||||
@@ -0,0 +1,115 @@
|
||||
package org.opencv.test.aruco;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.opencv.test.OpenCVTestCase;
|
||||
import org.junit.Assert;
|
||||
import org.opencv.core.Scalar;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfInt;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.objdetect.*;
|
||||
|
||||
|
||||
public class ArucoTest extends OpenCVTestCase {
|
||||
|
||||
public void testGenerateBoards() {
|
||||
Dictionary dictionary = Objdetect.getPredefinedDictionary(Objdetect.DICT_4X4_50);
|
||||
|
||||
Mat point1 = new Mat(4, 3, CvType.CV_32FC1);
|
||||
int row = 0, col = 0;
|
||||
double squareLength = 40.;
|
||||
point1.put(row, col, 0, 0, 0,
|
||||
0, squareLength, 0,
|
||||
squareLength, squareLength, 0,
|
||||
0, squareLength, 0);
|
||||
List<Mat>objPoints = new ArrayList<Mat>();
|
||||
objPoints.add(point1);
|
||||
|
||||
Mat ids = new Mat(1, 1, CvType.CV_32SC1);
|
||||
ids.put(row, col, 0);
|
||||
|
||||
Board board = new Board(objPoints, dictionary, ids);
|
||||
|
||||
Mat image = new Mat();
|
||||
board.generateImage(new Size(80, 80), image, 2);
|
||||
|
||||
assertTrue(image.total() > 0);
|
||||
}
|
||||
|
||||
public void testArucoIssue3133() {
|
||||
byte[][] marker = {{0,1,1},{1,1,1},{0,1,1}};
|
||||
Dictionary dictionary = Objdetect.extendDictionary(1, 3);
|
||||
dictionary.set_maxCorrectionBits(0);
|
||||
Mat markerBits = new Mat(3, 3, CvType.CV_8UC1);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
markerBits.put(i, j, marker[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
Mat markerCompressed = Dictionary.getByteListFromBits(markerBits);
|
||||
assertMatNotEqual(markerCompressed, dictionary.get_bytesList());
|
||||
|
||||
dictionary.set_bytesList(markerCompressed);
|
||||
assertMatEqual(markerCompressed, dictionary.get_bytesList());
|
||||
}
|
||||
|
||||
public void testArucoDetector() {
|
||||
Dictionary dictionary = Objdetect.getPredefinedDictionary(0);
|
||||
DetectorParameters detectorParameters = new DetectorParameters();
|
||||
ArucoDetector detector = new ArucoDetector(dictionary, detectorParameters);
|
||||
|
||||
Mat markerImage = new Mat();
|
||||
int id = 1, offset = 5, size = 40;
|
||||
Objdetect.generateImageMarker(dictionary, id, size, markerImage, detectorParameters.get_markerBorderBits());
|
||||
|
||||
Mat image = new Mat(markerImage.rows() + 2*offset, markerImage.cols() + 2*offset,
|
||||
CvType.CV_8UC1, new Scalar(255));
|
||||
Mat m = image.submat(offset, size+offset, offset, size+offset);
|
||||
markerImage.copyTo(m);
|
||||
|
||||
List<Mat> corners = new ArrayList();
|
||||
Mat ids = new Mat();
|
||||
detector.detectMarkers(image, corners, ids);
|
||||
|
||||
assertEquals(1, corners.size());
|
||||
Mat res = corners.get(0);
|
||||
assertArrayEquals(new double[]{offset, offset}, res.get(0, 0), 0.0);
|
||||
assertArrayEquals(new double[]{size + offset - 1, offset}, res.get(0, 1), 0.0);
|
||||
assertArrayEquals(new double[]{size + offset - 1, size + offset - 1}, res.get(0, 2), 0.0);
|
||||
assertArrayEquals(new double[]{offset, size + offset - 1}, res.get(0, 3), 0.0);
|
||||
}
|
||||
|
||||
public void testCharucoDetector() {
|
||||
Dictionary dictionary = Objdetect.getPredefinedDictionary(0);
|
||||
int boardSizeX = 3, boardSizeY = 3;
|
||||
CharucoBoard board = new CharucoBoard(new Size(boardSizeX, boardSizeY), 1.f, 0.8f, dictionary);
|
||||
CharucoDetector charucoDetector = new CharucoDetector(board);
|
||||
|
||||
int cellSize = 80;
|
||||
Mat boardImage = new Mat();
|
||||
board.generateImage(new Size(cellSize*boardSizeX, cellSize*boardSizeY), boardImage);
|
||||
|
||||
assertTrue(boardImage.total() > 0);
|
||||
|
||||
Mat charucoCorners = new Mat();
|
||||
Mat charucoIds = new Mat();
|
||||
charucoDetector.detectBoard(boardImage, charucoCorners, charucoIds);
|
||||
|
||||
assertEquals(4, charucoIds.total());
|
||||
int[] intCharucoIds = (new MatOfInt(charucoIds)).toArray();
|
||||
Assert.assertArrayEquals(new int[]{0, 1, 2, 3}, intCharucoIds);
|
||||
|
||||
// Note: Expected values adjusted by -0.5px after fixing the systematic offset bug in charuco_detector.cpp
|
||||
// The fix removes the incorrect +0.5 offset that was added after cornerSubPix
|
||||
double eps = 0.2;
|
||||
assertArrayEquals(new double[]{cellSize - 0.5, cellSize - 0.5}, charucoCorners.get(0, 0), eps);
|
||||
assertArrayEquals(new double[]{2*cellSize - 0.5, cellSize - 0.5}, charucoCorners.get(1, 0), eps);
|
||||
assertArrayEquals(new double[]{cellSize - 0.5, 2*cellSize - 0.5}, charucoCorners.get(2, 0), eps);
|
||||
assertArrayEquals(new double[]{2*cellSize - 0.5, 2*cellSize - 0.5}, charucoCorners.get(3, 0), eps);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.opencv.test.barcode;
|
||||
|
||||
import java.util.List;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.objdetect.BarcodeDetector;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.test.OpenCVTestCase;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BarcodeDetectorTest extends OpenCVTestCase {
|
||||
|
||||
private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
|
||||
private String testDataPath;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
// relys on https://developer.android.com/reference/java/lang/System
|
||||
isTestCaseEnabled = System.getProperties().getProperty("java.vm.name") != "Dalvik";
|
||||
|
||||
if (isTestCaseEnabled) {
|
||||
testDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
|
||||
if (testDataPath == null)
|
||||
throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
|
||||
}
|
||||
}
|
||||
|
||||
public void testDetectAndDecode() {
|
||||
Mat img = Imgcodecs.imread(testDataPath + "/cv/barcode/multiple/4_barcodes.jpg");
|
||||
assertFalse(img.empty());
|
||||
BarcodeDetector detector = new BarcodeDetector();
|
||||
assertNotNull(detector);
|
||||
List < String > infos = new ArrayList< String >();
|
||||
List < String > types = new ArrayList< String >();
|
||||
|
||||
boolean result = detector.detectAndDecodeWithType(img, infos, types);
|
||||
assertTrue(result);
|
||||
assertEquals(infos.size(), 4);
|
||||
assertEquals(types.size(), 4);
|
||||
final String[] correctResults = {"9787122276124", "9787118081473", "9787564350840", "9783319200064"};
|
||||
for (int i = 0; i < 4; i++) {
|
||||
assertEquals(types.get(i), "EAN_13");
|
||||
result = false;
|
||||
for (int j = 0; j < 4; j++) {
|
||||
if (correctResults[j].equals(infos.get(i))) {
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package org.opencv.test.objdetect;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfRect;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
import org.opencv.objdetect.CascadeClassifier;
|
||||
import org.opencv.objdetect.Objdetect;
|
||||
import org.opencv.test.OpenCVTestCase;
|
||||
import org.opencv.test.OpenCVTestRunner;
|
||||
|
||||
public class CascadeClassifierTest extends OpenCVTestCase {
|
||||
|
||||
private CascadeClassifier cc;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
cc = null;
|
||||
}
|
||||
|
||||
public void testCascadeClassifier() {
|
||||
cc = new CascadeClassifier();
|
||||
assertNotNull(cc);
|
||||
}
|
||||
|
||||
public void testCascadeClassifierString() {
|
||||
cc = new CascadeClassifier(OpenCVTestRunner.LBPCASCADE_FRONTALFACE_PATH);
|
||||
assertNotNull(cc);
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRect() {
|
||||
CascadeClassifier cc = new CascadeClassifier(OpenCVTestRunner.LBPCASCADE_FRONTALFACE_PATH);
|
||||
MatOfRect faces = new MatOfRect();
|
||||
|
||||
Mat greyLena = new Mat();
|
||||
Imgproc.cvtColor(rgbLena, greyLena, Imgproc.COLOR_RGB2GRAY);
|
||||
Imgproc.equalizeHist(greyLena, greyLena);
|
||||
|
||||
cc.detectMultiScale(greyLena, faces, 1.1, 3, Objdetect.CASCADE_SCALE_IMAGE, new Size(30, 30), new Size());
|
||||
assertEquals(1, faces.total());
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectDoubleInt() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectDoubleIntInt() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectDoubleIntIntSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectDoubleIntIntSizeSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfIntegerListOfDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfIntegerListOfDoubleDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfIntegerListOfDoubleDoubleInt() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfIntegerListOfDoubleDoubleIntInt() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfIntegerListOfDoubleDoubleIntIntSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfIntegerListOfDoubleDoubleIntIntSizeSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfIntegerListOfDoubleDoubleIntIntSizeSizeBoolean() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testEmpty() {
|
||||
cc = new CascadeClassifier();
|
||||
assertTrue(cc.empty());
|
||||
}
|
||||
|
||||
public void testLoad() {
|
||||
cc = new CascadeClassifier();
|
||||
cc.load(OpenCVTestRunner.LBPCASCADE_FRONTALFACE_PATH);
|
||||
assertFalse(cc.empty());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
package org.opencv.test.objdetect;
|
||||
|
||||
import org.opencv.objdetect.HOGDescriptor;
|
||||
import org.opencv.test.OpenCVTestCase;
|
||||
|
||||
public class HOGDescriptorTest extends OpenCVTestCase {
|
||||
|
||||
public void testCheckDetectorSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testComputeGradientMatMatMat() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testComputeGradientMatMatMatSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testComputeGradientMatMatMatSizeSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testComputeMatListOfFloat() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testComputeMatListOfFloatSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testComputeMatListOfFloatSizeSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testComputeMatListOfFloatSizeSizeListOfPoint() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMatListOfPoint() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMatListOfPointDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMatListOfPointDoubleSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMatListOfPointDoubleSizeSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMatListOfPointDoubleSizeSizeListOfPoint() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMatListOfPointListOfDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMatListOfPointListOfDoubleDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMatListOfPointListOfDoubleDoubleSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMatListOfPointListOfDoubleDoubleSizeSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMatListOfPointListOfDoubleDoubleSizeSizeListOfPoint() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRect() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectDoubleSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectDoubleSizeSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectDoubleSizeSizeDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectDoubleSizeSizeDoubleDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectDoubleSizeSizeDoubleDoubleBoolean() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfDoubleDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfDoubleDoubleSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfDoubleDoubleSizeSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfDoubleDoubleSizeSizeDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfDoubleDoubleSizeSizeDoubleDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testDetectMultiScaleMatListOfRectListOfDoubleDoubleSizeSizeDoubleDoubleBoolean() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGet_blockSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGet_blockStride() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGet_cellSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGet_derivAperture() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGet_gammaCorrection() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGet_histogramNormType() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGet_L2HysThreshold() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGet_nbins() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGet_nlevels() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGet_svmDetector() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGet_winSigma() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGet_winSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGetDaimlerPeopleDetector() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGetDefaultPeopleDetector() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGetDescriptorSize() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testGetWinSigma() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testHOGDescriptor() {
|
||||
HOGDescriptor hog = new HOGDescriptor();
|
||||
|
||||
assertNotNull(hog);
|
||||
assertEquals(HOGDescriptor.DEFAULT_NLEVELS, hog.get_nlevels());
|
||||
}
|
||||
|
||||
public void testHOGDescriptorSizeSizeSizeSizeInt() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testHOGDescriptorSizeSizeSizeSizeIntInt() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testHOGDescriptorSizeSizeSizeSizeIntIntDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testHOGDescriptorSizeSizeSizeSizeIntIntDoubleInt() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testHOGDescriptorSizeSizeSizeSizeIntIntDoubleIntDouble() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testHOGDescriptorSizeSizeSizeSizeIntIntDoubleIntDoubleBoolean() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testHOGDescriptorSizeSizeSizeSizeIntIntDoubleIntDoubleBooleanInt() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testHOGDescriptorString() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testLoadString() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testLoadStringString() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testSaveString() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testSaveStringString() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
public void testSetSVMDetector() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.opencv.test.objdetect;
|
||||
|
||||
import org.opencv.test.OpenCVTestCase;
|
||||
|
||||
public class ObjdetectTest extends OpenCVTestCase {
|
||||
|
||||
public void testGroupRectanglesListOfRectListOfIntegerInt() {
|
||||
fail("Not yet implemented");
|
||||
/*
|
||||
final int NUM = 10;
|
||||
MatOfRect rects = new MatOfRect();
|
||||
rects.alloc(NUM);
|
||||
|
||||
for (int i = 0; i < NUM; i++)
|
||||
rects.put(i, 0, 10, 10, 20, 20);
|
||||
|
||||
int groupThreshold = 1;
|
||||
Objdetect.groupRectangles(rects, null, groupThreshold);//TODO: second parameter should not be null
|
||||
assertEquals(1, rects.total());
|
||||
*/
|
||||
}
|
||||
|
||||
public void testGroupRectanglesListOfRectListOfIntegerIntDouble() {
|
||||
fail("Not yet implemented");
|
||||
/*
|
||||
final int NUM = 10;
|
||||
MatOfRect rects = new MatOfRect();
|
||||
rects.alloc(NUM);
|
||||
|
||||
for (int i = 0; i < NUM; i++)
|
||||
rects.put(i, 0, 10, 10, 20, 20);
|
||||
|
||||
for (int i = 0; i < NUM; i++)
|
||||
rects.put(i, 0, 10, 10, 25, 25);
|
||||
|
||||
int groupThreshold = 1;
|
||||
double eps = 0.2;
|
||||
Objdetect.groupRectangles(rects, null, groupThreshold, eps);//TODO: second parameter should not be null
|
||||
assertEquals(2, rects.size());
|
||||
*/
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package org.opencv.test.objdetect;
|
||||
|
||||
import java.util.List;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.objdetect.QRCodeDetector;
|
||||
import org.opencv.objdetect.QRCodeEncoder;
|
||||
import org.opencv.objdetect.QRCodeEncoder_Params;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
import org.opencv.test.OpenCVTestCase;
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public class QRCodeDetectorTest extends OpenCVTestCase {
|
||||
|
||||
private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
|
||||
private String testDataPath;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
// relys on https://developer.android.com/reference/java/lang/System
|
||||
isTestCaseEnabled = System.getProperties().getProperty("java.vm.name") != "Dalvik";
|
||||
|
||||
if (isTestCaseEnabled) {
|
||||
testDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
|
||||
if (testDataPath == null)
|
||||
throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
|
||||
}
|
||||
}
|
||||
|
||||
public void testDetectAndDecode() {
|
||||
Mat img = Imgcodecs.imread(testDataPath + "/cv/qrcode/link_ocv.jpg");
|
||||
assertFalse(img.empty());
|
||||
QRCodeDetector detector = new QRCodeDetector();
|
||||
assertNotNull(detector);
|
||||
String output = detector.detectAndDecode(img);
|
||||
assertEquals(output, "https://opencv.org/");
|
||||
}
|
||||
|
||||
public void testDetectAndDecodeMulti() {
|
||||
Mat img = Imgcodecs.imread(testDataPath + "/cv/qrcode/multiple/6_qrcodes.png");
|
||||
assertFalse(img.empty());
|
||||
QRCodeDetector detector = new QRCodeDetector();
|
||||
assertNotNull(detector);
|
||||
List < String > output = new ArrayList< String >();
|
||||
boolean result = detector.detectAndDecodeMulti(img, output);
|
||||
assertTrue(result);
|
||||
assertEquals(output.size(), 6);
|
||||
List < String > expectedResults = Arrays.asList("SKIP", "EXTRA", "TWO STEPS FORWARD", "STEP BACK", "QUESTION", "STEP FORWARD");
|
||||
assertEquals(new HashSet<String>(output), new HashSet<String>(expectedResults));
|
||||
}
|
||||
|
||||
public void testKanji() {
|
||||
byte[] inp = new byte[]{(byte)0x82, (byte)0xb1, (byte)0x82, (byte)0xf1, (byte)0x82, (byte)0xc9, (byte)0x82,
|
||||
(byte)0xbf, (byte)0x82, (byte)0xcd, (byte)0x90, (byte)0xa2, (byte)0x8a, (byte)0x45};
|
||||
QRCodeEncoder_Params params = new QRCodeEncoder_Params();
|
||||
params.set_mode(QRCodeEncoder.MODE_KANJI);
|
||||
QRCodeEncoder encoder = QRCodeEncoder.create(params);
|
||||
|
||||
Mat qrcode = new Mat();
|
||||
encoder.encode(inp, qrcode);
|
||||
Imgproc.resize(qrcode, qrcode, new Size(0, 0), 2, 2, Imgproc.INTER_NEAREST);
|
||||
|
||||
QRCodeDetector detector = new QRCodeDetector();
|
||||
byte[] output = detector.detectAndDecodeBytes(qrcode);
|
||||
assertEquals(detector.getEncoding(), QRCodeEncoder.ECI_SHIFT_JIS);
|
||||
assertArrayEquals(inp, output);
|
||||
|
||||
List < byte[] > outputs = new ArrayList< byte[] >();
|
||||
assertTrue(detector.detectAndDecodeBytesMulti(qrcode, outputs));
|
||||
assertEquals(detector.getEncoding(0), QRCodeEncoder.ECI_SHIFT_JIS);
|
||||
assertArrayEquals(inp, outputs.get(0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"whitelist":
|
||||
{
|
||||
"": ["groupRectangles", "getPredefinedDictionary", "extendDictionary", "drawDetectedMarkers", "generateImageMarker", "drawDetectedCornersCharuco", "drawDetectedDiamonds"],
|
||||
"HOGDescriptor": ["load", "HOGDescriptor", "getDefaultPeopleDetector", "getDaimlerPeopleDetector", "setSVMDetector", "detectMultiScale"],
|
||||
"CascadeClassifier": ["load", "detectMultiScale2", "CascadeClassifier", "detectMultiScale3", "empty", "detectMultiScale"],
|
||||
"GraphicalCodeDetector": ["decode", "detect", "detectAndDecode", "detectMulti", "decodeMulti", "detectAndDecodeMulti"],
|
||||
"QRCodeDetector": ["QRCodeDetector", "decode", "detect", "detectAndDecode", "detectMulti", "decodeMulti", "detectAndDecodeMulti", "decodeCurved", "detectAndDecodeCurved", "setEpsX", "setEpsY"],
|
||||
"aruco_PredefinedDictionaryType": [],
|
||||
"aruco_Dictionary": ["Dictionary", "getDistanceToId", "generateImageMarker", "getByteListFromBits", "getBitsFromByteList"],
|
||||
"aruco_Board": ["Board", "matchImagePoints", "generateImage"],
|
||||
"aruco_GridBoard": ["GridBoard", "generateImage", "getGridSize", "getMarkerLength", "getMarkerSeparation", "matchImagePoints"],
|
||||
"aruco_CharucoParameters": ["CharucoParameters"],
|
||||
"aruco_CharucoBoard": ["CharucoBoard", "generateImage", "getChessboardCorners", "getNearestMarkerCorners", "checkCharucoCornersCollinear", "matchImagePoints", "getLegacyPattern", "setLegacyPattern"],
|
||||
"aruco_DetectorParameters": ["DetectorParameters"],
|
||||
"aruco_RefineParameters": ["RefineParameters"],
|
||||
"aruco_ArucoDetector": ["ArucoDetector", "detectMarkers", "refineDetectedMarkers", "setDictionary", "setDetectorParameters", "setRefineParameters"],
|
||||
"aruco_CharucoDetector": ["CharucoDetector", "setBoard", "setCharucoParameters", "setDetectorParameters", "setRefineParameters", "detectBoard", "detectDiamonds"],
|
||||
"QRCodeDetectorAruco_Params": ["Params"],
|
||||
"QRCodeDetectorAruco": ["QRCodeDetectorAruco", "decode", "detect", "detectAndDecode", "detectMulti", "decodeMulti", "detectAndDecodeMulti", "setDetectorParameters", "setArucoParameters"],
|
||||
"barcode_BarcodeDetector": ["BarcodeDetector", "decode", "detect", "detectAndDecode", "detectMulti", "decodeMulti", "detectAndDecodeMulti", "decodeWithType", "detectAndDecodeWithType"],
|
||||
"FaceDetectorYN": ["setInputSize", "getInputSize", "setScoreThreshold", "getScoreThreshold", "setNMSThreshold", "getNMSThreshold", "setTopK", "getTopK", "detect", "create"]
|
||||
},
|
||||
"namespace_prefix_override":
|
||||
{
|
||||
"aruco": ""
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"ManualFuncs" : {
|
||||
"QRCodeDetectorAruco": {
|
||||
"getDetectorParameters": { "declaration" : [""], "implementation" : [""] }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifdef HAVE_OPENCV_OBJDETECT
|
||||
|
||||
#include "opencv2/objdetect.hpp"
|
||||
|
||||
typedef QRCodeEncoder::Params QRCodeEncoder_Params;
|
||||
|
||||
typedef HOGDescriptor::HistogramNormType HOGDescriptor_HistogramNormType;
|
||||
typedef HOGDescriptor::DescriptorStorageFormat HOGDescriptor_DescriptorStorageFormat;
|
||||
|
||||
class NativeByteArray
|
||||
{
|
||||
public:
|
||||
inline NativeByteArray& operator=(const std::string& from) {
|
||||
val = from;
|
||||
return *this;
|
||||
}
|
||||
std::string val;
|
||||
};
|
||||
|
||||
class vector_NativeByteArray : public std::vector<std::string> {};
|
||||
|
||||
template<>
|
||||
PyObject* pyopencv_from(const NativeByteArray& from)
|
||||
{
|
||||
return PyBytes_FromStringAndSize(from.val.c_str(), from.val.size());
|
||||
}
|
||||
|
||||
template<>
|
||||
PyObject* pyopencv_from(const vector_NativeByteArray& results)
|
||||
{
|
||||
PyObject* list = PyList_New(results.size());
|
||||
for(size_t i = 0; i < results.size(); ++i)
|
||||
PyList_SetItem(list, i, PyBytes_FromStringAndSize(results[i].c_str(), results[i].size()));
|
||||
return list;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python
|
||||
'''
|
||||
===============================================================================
|
||||
Barcode detect and decode pipeline.
|
||||
===============================================================================
|
||||
'''
|
||||
import os
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
from tests_common import NewOpenCVTests
|
||||
|
||||
class barcode_detector_test(NewOpenCVTests):
|
||||
|
||||
def test_detect(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/barcode/multiple/4_barcodes.jpg'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.barcode_BarcodeDetector()
|
||||
retval, corners = detector.detect(img)
|
||||
self.assertTrue(retval)
|
||||
self.assertEqual(corners.shape, (4, 4, 2))
|
||||
|
||||
def test_detect_and_decode(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/barcode/single/book.jpg'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.barcode_BarcodeDetector()
|
||||
retval, decoded_info, decoded_type, corners = detector.detectAndDecodeWithType(img)
|
||||
self.assertTrue(retval)
|
||||
self.assertTrue(len(decoded_info) > 0)
|
||||
self.assertTrue(len(decoded_type) > 0)
|
||||
self.assertEqual(decoded_info[0], "9787115279460")
|
||||
self.assertEqual(decoded_type[0], "EAN_13")
|
||||
self.assertEqual(corners.shape, (1, 4, 2))
|
||||
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
'''
|
||||
face detection using haar cascades
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
def detect(img, cascade):
|
||||
rects = cascade.detectMultiScale(img, scaleFactor=1.275, minNeighbors=4, minSize=(30, 30),
|
||||
flags=cv.CASCADE_SCALE_IMAGE)
|
||||
if len(rects) == 0:
|
||||
return []
|
||||
rects[:,2:] += rects[:,:2]
|
||||
return rects
|
||||
|
||||
from tests_common import NewOpenCVTests, intersectionRate
|
||||
|
||||
class facedetect_test(NewOpenCVTests):
|
||||
|
||||
def test_facedetect(self):
|
||||
cascade_fn = self.repoPath + '/data/haarcascades/haarcascade_frontalface_alt.xml'
|
||||
nested_fn = self.repoPath + '/data/haarcascades/haarcascade_eye.xml'
|
||||
|
||||
cascade = cv.CascadeClassifier(cascade_fn)
|
||||
nested = cv.CascadeClassifier(nested_fn)
|
||||
|
||||
samples = ['samples/data/lena.jpg', 'cv/cascadeandhog/images/mona-lisa.png']
|
||||
|
||||
faces = []
|
||||
eyes = []
|
||||
|
||||
testFaces = [
|
||||
#lena
|
||||
[[218, 200, 389, 371],
|
||||
[ 244, 240, 294, 290],
|
||||
[ 309, 246, 352, 289]],
|
||||
|
||||
#lisa
|
||||
[[167, 119, 307, 259],
|
||||
[188, 153, 229, 194],
|
||||
[236, 153, 277, 194]]
|
||||
]
|
||||
|
||||
for sample in samples:
|
||||
|
||||
img = self.get_sample( sample)
|
||||
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
|
||||
gray = cv.GaussianBlur(gray, (5, 5), 0)
|
||||
|
||||
rects = detect(gray, cascade)
|
||||
faces.append(rects)
|
||||
|
||||
if not nested.empty():
|
||||
for x1, y1, x2, y2 in rects:
|
||||
roi = gray[y1:y2, x1:x2]
|
||||
subrects = detect(roi.copy(), nested)
|
||||
|
||||
for rect in subrects:
|
||||
rect[0] += x1
|
||||
rect[2] += x1
|
||||
rect[1] += y1
|
||||
rect[3] += y1
|
||||
|
||||
eyes.append(subrects)
|
||||
|
||||
faces_matches = 0
|
||||
eyes_matches = 0
|
||||
|
||||
eps = 0.8
|
||||
|
||||
for i in range(len(faces)):
|
||||
for j in range(len(testFaces)):
|
||||
if intersectionRate(faces[i][0], testFaces[j][0]) > eps:
|
||||
faces_matches += 1
|
||||
#check eyes
|
||||
if len(eyes[i]) == 2:
|
||||
if intersectionRate(eyes[i][0], testFaces[j][1]) > eps and intersectionRate(eyes[i][1] , testFaces[j][2]) > eps:
|
||||
eyes_matches += 1
|
||||
elif intersectionRate(eyes[i][1], testFaces[j][1]) > eps and intersectionRate(eyes[i][0], testFaces[j][2]) > eps:
|
||||
eyes_matches += 1
|
||||
|
||||
self.assertEqual(faces_matches, 2)
|
||||
self.assertEqual(eyes_matches, 2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
NewOpenCVTests.bootstrap()
|
||||
@@ -0,0 +1,520 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
|
||||
import os, tempfile, numpy as np
|
||||
from math import pi
|
||||
|
||||
import cv2 as cv
|
||||
|
||||
from tests_common import NewOpenCVTests
|
||||
|
||||
def getSyntheticRT(yaw, pitch, distance):
|
||||
rvec = np.zeros((3, 1), np.float64)
|
||||
tvec = np.zeros((3, 1), np.float64)
|
||||
|
||||
rotPitch = np.array([[-pitch], [0], [0]])
|
||||
rotYaw = np.array([[0], [yaw], [0]])
|
||||
|
||||
rvec, tvec = cv.composeRT(rotPitch, np.zeros((3, 1), np.float64),
|
||||
rotYaw, np.zeros((3, 1), np.float64))[:2]
|
||||
|
||||
tvec = np.array([[0], [0], [distance]])
|
||||
return rvec, tvec
|
||||
|
||||
# see test_aruco_utils.cpp
|
||||
def projectMarker(img, board, markerIndex, cameraMatrix, rvec, tvec, markerBorder):
|
||||
markerSizePixels = 100
|
||||
markerImg = cv.aruco.generateImageMarker(board.getDictionary(), board.getIds()[markerIndex], markerSizePixels, borderBits=markerBorder)
|
||||
|
||||
distCoeffs = np.zeros((5, 1), np.float64)
|
||||
maxCoord = board.getRightBottomCorner()
|
||||
objPoints = board.getObjPoints()[markerIndex]
|
||||
for i in range(len(objPoints)):
|
||||
objPoints[i][0] -= maxCoord[0] / 2
|
||||
objPoints[i][1] -= maxCoord[1] / 2
|
||||
objPoints[i][2] -= maxCoord[2] / 2
|
||||
|
||||
corners, _ = cv.projectPoints(objPoints, rvec, tvec, cameraMatrix, distCoeffs)
|
||||
|
||||
originalCorners = np.array([
|
||||
[0, 0],
|
||||
[markerSizePixels, 0],
|
||||
[markerSizePixels, markerSizePixels],
|
||||
[0, markerSizePixels],
|
||||
], np.float32)
|
||||
|
||||
transformation = cv.getPerspectiveTransform(originalCorners, corners)
|
||||
|
||||
borderValue = 127
|
||||
aux = cv.warpPerspective(markerImg, transformation, img.shape, None, cv.INTER_NEAREST, cv.BORDER_CONSTANT, borderValue)
|
||||
|
||||
assert(img.shape == aux.shape)
|
||||
mask = (aux == borderValue).astype(np.uint8)
|
||||
img = img * mask + aux * (1 - mask)
|
||||
return img
|
||||
|
||||
def projectChessboard(squaresX, squaresY, squareSize, imageSize, cameraMatrix, rvec, tvec):
|
||||
img = np.ones(imageSize, np.uint8) * 255
|
||||
distCoeffs = np.zeros((5, 1), np.float64)
|
||||
for y in range(squaresY):
|
||||
startY = y * squareSize
|
||||
for x in range(squaresX):
|
||||
if (y % 2 != x % 2):
|
||||
continue
|
||||
startX = x * squareSize
|
||||
|
||||
squareCorners = np.array([[startX - squaresX*squareSize/2,
|
||||
startY - squaresY*squareSize/2,
|
||||
0]], np.float32)
|
||||
squareCorners = np.stack((squareCorners[0],
|
||||
squareCorners[0] + [squareSize, 0, 0],
|
||||
squareCorners[0] + [squareSize, squareSize, 0],
|
||||
squareCorners[0] + [0, squareSize, 0]))
|
||||
|
||||
projectedCorners, _ = cv.projectPoints(squareCorners, rvec, tvec, cameraMatrix, distCoeffs)
|
||||
projectedCorners = projectedCorners.astype(np.int64)
|
||||
projectedCorners = projectedCorners.reshape(1, 4, 2)
|
||||
img = cv.fillPoly(img, [projectedCorners], 0)
|
||||
|
||||
return img
|
||||
|
||||
def projectCharucoBoard(board, cameraMatrix, yaw, pitch, distance, imageSize, markerBorder):
|
||||
rvec, tvec = getSyntheticRT(yaw, pitch, distance)
|
||||
|
||||
img = np.ones(imageSize, np.uint8) * 255
|
||||
for indexMarker in range(len(board.getIds())):
|
||||
img = projectMarker(img, board, indexMarker, cameraMatrix, rvec, tvec, markerBorder)
|
||||
|
||||
chessboard = projectChessboard(board.getChessboardSize()[0], board.getChessboardSize()[1],
|
||||
board.getSquareLength(), imageSize, cameraMatrix, rvec, tvec)
|
||||
|
||||
chessboard = (chessboard != 0).astype(np.uint8)
|
||||
img = img * chessboard
|
||||
return img, rvec, tvec
|
||||
|
||||
class aruco_objdetect_test(NewOpenCVTests):
|
||||
|
||||
def test_board(self):
|
||||
p1 = np.array([[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]], dtype=np.float32)
|
||||
p2 = np.array([[1, 0, 0], [1, 1, 0], [2, 1, 0], [2, 0, 0]], dtype=np.float32)
|
||||
objPoints = np.array([p1, p2])
|
||||
dictionary = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_50)
|
||||
ids = np.array([0, 1])
|
||||
|
||||
board = cv.aruco.Board(objPoints, dictionary, ids)
|
||||
np.testing.assert_array_equal(board.getIds().squeeze(), ids)
|
||||
np.testing.assert_array_equal(np.ravel(np.array(board.getObjPoints())), np.ravel(np.concatenate([p1, p2])))
|
||||
|
||||
def test_idsAccessibility(self):
|
||||
|
||||
ids = np.arange(17)
|
||||
rev_ids = ids[::-1]
|
||||
|
||||
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_5X5_250)
|
||||
board = cv.aruco.CharucoBoard((7, 5), 1, 0.5, aruco_dict)
|
||||
|
||||
np.testing.assert_array_equal(board.getIds().squeeze(), ids)
|
||||
|
||||
board = cv.aruco.CharucoBoard((7, 5), 1, 0.5, aruco_dict, rev_ids)
|
||||
np.testing.assert_array_equal(board.getIds().squeeze(), rev_ids)
|
||||
|
||||
board = cv.aruco.CharucoBoard((7, 5), 1, 0.5, aruco_dict, ids)
|
||||
np.testing.assert_array_equal(board.getIds().squeeze(), ids)
|
||||
|
||||
def test_identify(self):
|
||||
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_50)
|
||||
expected_idx = 9
|
||||
expected_rotation = 2
|
||||
bit_marker = np.array([[0, 1, 1, 0], [1, 0, 1, 0], [1, 1, 1, 1], [0, 0, 1, 1]], dtype=np.uint8)
|
||||
|
||||
check, idx, rotation = aruco_dict.identify(bit_marker, 0)
|
||||
|
||||
self.assertTrue(check, True)
|
||||
self.assertEqual(idx, expected_idx)
|
||||
self.assertEqual(rotation, expected_rotation)
|
||||
|
||||
def test_getDistanceToId(self):
|
||||
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_50)
|
||||
idx = 7
|
||||
rotation = 3
|
||||
bit_marker = np.array([[0, 1, 0, 1], [0, 1, 1, 1], [1, 1, 0, 0], [0, 1, 0, 0]], dtype=np.uint8)
|
||||
dist = aruco_dict.getDistanceToId(bit_marker, idx)
|
||||
|
||||
self.assertEqual(dist, 0)
|
||||
|
||||
def test_getDistanceToId_cell_pixel_ratio(self):
|
||||
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_50)
|
||||
idx = 7
|
||||
valid_bit_id_threshold = 0.49
|
||||
bit_marker = np.array([[0, 1, 0, 1], [0, 1, 1, 1], [1, 1, 0, 0], [0, 1, 0, 0]], dtype=np.uint8)
|
||||
ratio_marker = bit_marker.astype(np.float32)
|
||||
|
||||
# Same marker as test_getDistanceToId, but passed as float cell ratios.
|
||||
dist = aruco_dict.getDistanceToId(ratio_marker, idx, True, valid_bit_id_threshold)
|
||||
self.assertEqual(dist, 0)
|
||||
|
||||
# A small drift stays within the threshold.
|
||||
accepted_ratio = ratio_marker.copy()
|
||||
accepted_ratio[0, 0] = 0.4
|
||||
dist = aruco_dict.getDistanceToId(accepted_ratio, idx, True, valid_bit_id_threshold)
|
||||
self.assertEqual(dist, 0)
|
||||
|
||||
# A full flip crosses the threshold and counts as one bad cell.
|
||||
erroneous_ratio = ratio_marker.copy()
|
||||
erroneous_ratio[0, 0] = 1.0 - erroneous_ratio[0, 0]
|
||||
dist = aruco_dict.getDistanceToId(onlyCellPixelRatio=erroneous_ratio,
|
||||
id=idx,
|
||||
allRotations=True,
|
||||
validBitIdThreshold=valid_bit_id_threshold)
|
||||
self.assertEqual(dist, 1)
|
||||
|
||||
def test_aruco_detector(self):
|
||||
aruco_params = cv.aruco.DetectorParameters()
|
||||
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_250)
|
||||
aruco_detector = cv.aruco.ArucoDetector(aruco_dict, aruco_params)
|
||||
id = 2
|
||||
marker_size = 100
|
||||
offset = 10
|
||||
img_marker = cv.aruco.generateImageMarker(aruco_dict, id, marker_size, aruco_params.markerBorderBits)
|
||||
img_marker = np.pad(img_marker, pad_width=offset, mode='constant', constant_values=255)
|
||||
gold_corners = np.array([[offset, offset],[marker_size+offset-1.0,offset],
|
||||
[marker_size+offset-1.0,marker_size+offset-1.0],
|
||||
[offset, marker_size+offset-1.0]], dtype=np.float32)
|
||||
corners, ids, rejected = aruco_detector.detectMarkers(img_marker)
|
||||
|
||||
self.assertEqual(1, len(ids))
|
||||
self.assertEqual(id, ids[0])
|
||||
for i in range(0, len(corners)):
|
||||
np.testing.assert_array_equal(gold_corners, corners[i].reshape(4, 2))
|
||||
|
||||
def test_aruco_detector_refine(self):
|
||||
aruco_params = cv.aruco.DetectorParameters()
|
||||
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_250)
|
||||
aruco_detector = cv.aruco.ArucoDetector(aruco_dict, aruco_params)
|
||||
board_size = (3, 4)
|
||||
board = cv.aruco.GridBoard(board_size, 5.0, 1.0, aruco_dict)
|
||||
board_image = board.generateImage((board_size[0]*50, board_size[1]*50), marginSize=10)
|
||||
|
||||
corners, ids, rejected = aruco_detector.detectMarkers(board_image)
|
||||
self.assertEqual(board_size[0]*board_size[1], len(ids))
|
||||
|
||||
part_corners, part_ids, part_rejected = corners[:-1], ids[:-1], list(rejected)
|
||||
part_rejected.append(corners[-1])
|
||||
|
||||
refine_corners, refine_ids, refine_rejected, recovered_ids = aruco_detector.refineDetectedMarkers(board_image, board, part_corners, part_ids, part_rejected)
|
||||
|
||||
self.assertEqual(board_size[0] * board_size[1], len(refine_ids))
|
||||
self.assertEqual(1, len(recovered_ids))
|
||||
|
||||
self.assertEqual(ids[-1], refine_ids[-1])
|
||||
self.assertEqual((1, 4, 2), refine_corners[0].shape)
|
||||
np.testing.assert_array_equal(corners, refine_corners)
|
||||
|
||||
def test_charuco_refine(self):
|
||||
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_6X6_50)
|
||||
board_size = (3, 4)
|
||||
board = cv.aruco.CharucoBoard(board_size, 1., .7, aruco_dict)
|
||||
aruco_detector = cv.aruco.ArucoDetector(aruco_dict)
|
||||
charuco_detector = cv.aruco.CharucoDetector(board)
|
||||
cell_size = 100
|
||||
image = board.generateImage((cell_size*board_size[0], cell_size*board_size[1]))
|
||||
camera = np.array([[1, 0, 0.5],
|
||||
[0, 1, 0.5],
|
||||
[0, 0, 1]])
|
||||
dist = np.array([0, 0, 0, 0, 0], dtype=np.float32).reshape(1, -1)
|
||||
|
||||
# generate gold corners of the ArUco markers for the test
|
||||
gold_corners = np.array(board.getObjPoints())[:, :, 0:2]*cell_size
|
||||
|
||||
# detect corners
|
||||
markerCorners, markerIds, _ = aruco_detector.detectMarkers(image)
|
||||
|
||||
# test refine
|
||||
rejected = [markerCorners[-1]]
|
||||
markerCorners, markerIds = markerCorners[:-1], markerIds[:-1]
|
||||
markerCorners, markerIds, _, _ = aruco_detector.refineDetectedMarkers(image, board, markerCorners, markerIds,
|
||||
rejected, cameraMatrix=camera, distCoeffs=dist)
|
||||
|
||||
charucoCorners, charucoIds, _, _ = charuco_detector.detectBoard(image, markerCorners=markerCorners,
|
||||
markerIds=markerIds)
|
||||
self.assertEqual(len(charucoIds), 6)
|
||||
self.assertEqual(len(markerIds), 6)
|
||||
|
||||
for i, id in enumerate(markerIds.reshape(-1)):
|
||||
np.testing.assert_allclose(gold_corners[id], markerCorners[i].reshape(4, 2), 0.01, 1.)
|
||||
|
||||
def test_write_read_dictionary(self):
|
||||
try:
|
||||
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_5X5_50)
|
||||
markers_gold = aruco_dict.bytesList
|
||||
|
||||
# write aruco_dict
|
||||
fd, filename = tempfile.mkstemp(prefix="opencv_python_aruco_dict_", suffix=".yml")
|
||||
os.close(fd)
|
||||
|
||||
fs_write = cv.FileStorage(filename, cv.FileStorage_WRITE)
|
||||
aruco_dict.writeDictionary(fs_write)
|
||||
fs_write.release()
|
||||
|
||||
# reset aruco_dict
|
||||
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_6X6_250)
|
||||
|
||||
# read aruco_dict
|
||||
fs_read = cv.FileStorage(filename, cv.FileStorage_READ)
|
||||
aruco_dict.readDictionary(fs_read.root())
|
||||
fs_read.release()
|
||||
|
||||
# check equal
|
||||
self.assertEqual(aruco_dict.markerSize, 5)
|
||||
self.assertEqual(aruco_dict.maxCorrectionBits, 3)
|
||||
np.testing.assert_array_equal(aruco_dict.bytesList, markers_gold)
|
||||
|
||||
finally:
|
||||
if os.path.exists(filename):
|
||||
os.remove(filename)
|
||||
|
||||
def test_charuco_detector(self):
|
||||
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_250)
|
||||
board_size = (3, 3)
|
||||
board = cv.aruco.CharucoBoard(board_size, 1.0, .8, aruco_dict)
|
||||
charuco_detector = cv.aruco.CharucoDetector(board)
|
||||
cell_size = 100
|
||||
|
||||
image = board.generateImage((cell_size*board_size[0], cell_size*board_size[1]))
|
||||
|
||||
# Note: Expected values adjusted by -0.5px after fixing the systematic offset bug in charuco_detector.cpp
|
||||
# The fix removes the incorrect +0.5 offset that was added after cornerSubPix
|
||||
list_gold_corners = []
|
||||
for i in range(1, board_size[0]):
|
||||
for j in range(1, board_size[1]):
|
||||
list_gold_corners.append((j*cell_size - 0.5, i*cell_size - 0.5))
|
||||
gold_corners = np.array(list_gold_corners, dtype=np.float32)
|
||||
|
||||
charucoCorners, charucoIds, markerCorners, markerIds = charuco_detector.detectBoard(image)
|
||||
|
||||
self.assertEqual(len(charucoIds), 4)
|
||||
for i in range(0, 4):
|
||||
self.assertEqual(charucoIds[i], i)
|
||||
np.testing.assert_allclose(gold_corners, charucoCorners.reshape(-1, 2), 0.01, 0.1)
|
||||
|
||||
def test_detect_diamonds(self):
|
||||
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_6X6_250)
|
||||
board_size = (3, 3)
|
||||
board = cv.aruco.CharucoBoard(board_size, 1.0, .8, aruco_dict)
|
||||
charuco_detector = cv.aruco.CharucoDetector(board)
|
||||
cell_size = 120
|
||||
|
||||
image = board.generateImage((cell_size*board_size[0], cell_size*board_size[1]))
|
||||
|
||||
# Note: Expected values adjusted by -0.5px after fixing the systematic offset bug in charuco_detector.cpp
|
||||
# The fix removes the incorrect +0.5 offset that was added after cornerSubPix
|
||||
list_gold_corners = [(cell_size - 0.5, cell_size - 0.5), (2*cell_size - 0.5, cell_size - 0.5),
|
||||
(2*cell_size - 0.5, 2*cell_size - 0.5), (cell_size - 0.5, 2*cell_size - 0.5)]
|
||||
gold_corners = np.array(list_gold_corners, dtype=np.float32)
|
||||
|
||||
diamond_corners, diamond_ids, marker_corners, marker_ids = charuco_detector.detectDiamonds(image)
|
||||
|
||||
self.assertEqual(diamond_ids.size, 4)
|
||||
self.assertEqual(marker_ids.size, 4)
|
||||
for i in range(0, 4):
|
||||
self.assertEqual(diamond_ids[0][0][i], i)
|
||||
np.testing.assert_allclose(gold_corners, np.array(diamond_corners, dtype=np.float32).reshape(-1, 2), 0.01, 0.1)
|
||||
|
||||
# check no segfault when cameraMatrix or distCoeffs are not initialized
|
||||
def test_charuco_no_segfault_params(self):
|
||||
dictionary = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_1000)
|
||||
board = cv.aruco.CharucoBoard((10, 10), 0.019, 0.015, dictionary)
|
||||
charuco_parameters = cv.aruco.CharucoParameters()
|
||||
detector = cv.aruco.CharucoDetector(board)
|
||||
detector.setCharucoParameters(charuco_parameters)
|
||||
|
||||
self.assertIsNone(detector.getCharucoParameters().cameraMatrix)
|
||||
self.assertIsNone(detector.getCharucoParameters().distCoeffs)
|
||||
|
||||
def test_charuco_no_segfault_params_constructor(self):
|
||||
dictionary = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_1000)
|
||||
board = cv.aruco.CharucoBoard((10, 10), 0.019, 0.015, dictionary)
|
||||
charuco_parameters = cv.aruco.CharucoParameters()
|
||||
detector = cv.aruco.CharucoDetector(board, charucoParams=charuco_parameters)
|
||||
|
||||
self.assertIsNone(detector.getCharucoParameters().cameraMatrix)
|
||||
self.assertIsNone(detector.getCharucoParameters().distCoeffs)
|
||||
|
||||
# similar to C++ test CV_CharucoDetection.accuracy
|
||||
def test_charuco_detector_accuracy(self):
|
||||
iteration = 0
|
||||
cameraMatrix = np.eye(3, 3, dtype=np.float64)
|
||||
imgSize = (500, 500)
|
||||
params = cv.aruco.DetectorParameters()
|
||||
params.minDistanceToBorder = 3
|
||||
params.validBitIdThreshold = 0.5
|
||||
|
||||
board = cv.aruco.CharucoBoard((4, 4), 0.03, 0.015, cv.aruco.getPredefinedDictionary(cv.aruco.DICT_6X6_250))
|
||||
detector = cv.aruco.CharucoDetector(board, detectorParams=params)
|
||||
|
||||
cameraMatrix[0, 0] = cameraMatrix[1, 1] = 600
|
||||
cameraMatrix[0, 2] = imgSize[0] / 2
|
||||
cameraMatrix[1, 2] = imgSize[1] / 2
|
||||
|
||||
# for different perspectives
|
||||
distCoeffs = np.zeros((5, 1), dtype=np.float64)
|
||||
for distance in [0.2, 0.4]:
|
||||
for yaw in range(-55, 51, 25):
|
||||
for pitch in range(-55, 51, 25):
|
||||
markerBorder = iteration % 2 + 1
|
||||
iteration += 1
|
||||
|
||||
# create synthetic image
|
||||
img, rvec, tvec = projectCharucoBoard(board, cameraMatrix, yaw * pi / 180, pitch * pi / 180, distance, imgSize, markerBorder)
|
||||
|
||||
params.markerBorderBits = markerBorder
|
||||
detector.setDetectorParameters(params)
|
||||
|
||||
if (iteration % 2 != 0):
|
||||
charucoParameters = cv.aruco.CharucoParameters()
|
||||
charucoParameters.cameraMatrix = cameraMatrix
|
||||
charucoParameters.distCoeffs = distCoeffs
|
||||
detector.setCharucoParameters(charucoParameters)
|
||||
|
||||
charucoCorners, charucoIds, corners, ids = detector.detectBoard(img)
|
||||
|
||||
self.assertGreater(len(ids), 0)
|
||||
|
||||
copyChessboardCorners = board.getChessboardCorners()
|
||||
copyChessboardCorners -= np.array(board.getRightBottomCorner()) / 2
|
||||
|
||||
projectedCharucoCorners, _ = cv.projectPoints(copyChessboardCorners, rvec, tvec, cameraMatrix, distCoeffs)
|
||||
|
||||
if charucoIds is None:
|
||||
# Detection can fail at extreme viewing angles
|
||||
self.assertTrue(abs(yaw) >= 45 or abs(pitch) >= 45,
|
||||
f"Detection failed unexpectedly at yaw={yaw}, pitch={pitch}")
|
||||
continue
|
||||
|
||||
for i in range(len(charucoIds)):
|
||||
currentId = charucoIds[i]
|
||||
self.assertLess(currentId, len(board.getChessboardCorners()))
|
||||
|
||||
reprErr = cv.norm(charucoCorners[i] - projectedCharucoCorners[currentId])
|
||||
self.assertLessEqual(reprErr, 5)
|
||||
|
||||
def test_aruco_match_image_points(self):
|
||||
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_50)
|
||||
board_size = (3, 4)
|
||||
board = cv.aruco.GridBoard(board_size, 5.0, 1.0, aruco_dict)
|
||||
aruco_corners = np.array(board.getObjPoints())[:, :, :2]
|
||||
aruco_ids = board.getIds()
|
||||
obj_points, img_points = board.matchImagePoints(aruco_corners, aruco_ids)
|
||||
aruco_corners = aruco_corners.reshape(-1, 2)
|
||||
|
||||
self.assertEqual(aruco_corners.shape[0], obj_points.shape[0])
|
||||
self.assertEqual(img_points.shape[0], obj_points.shape[0])
|
||||
self.assertEqual(2, img_points.shape[2])
|
||||
np.testing.assert_array_equal(aruco_corners, obj_points[:, :, :2].reshape(-1, 2))
|
||||
|
||||
def test_charuco_match_image_points(self):
|
||||
aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_50)
|
||||
board_size = (3, 4)
|
||||
board = cv.aruco.CharucoBoard(board_size, 5.0, 1.0, aruco_dict)
|
||||
chessboard_corners = np.array(board.getChessboardCorners())[:, :2]
|
||||
chessboard_ids = board.getIds()
|
||||
obj_points, img_points = board.matchImagePoints(chessboard_corners, chessboard_ids)
|
||||
|
||||
self.assertEqual(chessboard_corners.shape[0], obj_points.shape[0])
|
||||
self.assertEqual(img_points.shape[0], obj_points.shape[0])
|
||||
self.assertEqual(2, img_points.shape[2])
|
||||
np.testing.assert_array_equal(chessboard_corners, obj_points[:, :, :2].reshape(-1, 2))
|
||||
|
||||
def test_draw_detected_markers(self):
|
||||
detected_points = [[[10, 10], [50, 10], [50, 50], [10, 50]]]
|
||||
img = np.zeros((60, 60), dtype=np.uint8)
|
||||
|
||||
# add extra dimension in Python to create Nx4 Mat with 2 channels
|
||||
points1 = np.array(detected_points).reshape(-1, 4, 1, 2)
|
||||
img = cv.aruco.drawDetectedMarkers(img, points1, borderColor=255)
|
||||
|
||||
# check that the marker borders are painted
|
||||
contours, _ = cv.findContours(img, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
|
||||
self.assertEqual(len(contours), 1)
|
||||
self.assertEqual(img[10, 10], 255)
|
||||
self.assertEqual(img[50, 10], 255)
|
||||
self.assertEqual(img[50, 50], 255)
|
||||
self.assertEqual(img[10, 50], 255)
|
||||
|
||||
# must throw Exception without extra dimension
|
||||
points2 = np.array(detected_points)
|
||||
with self.assertRaises(Exception):
|
||||
img = cv.aruco.drawDetectedMarkers(img, points2, borderColor=255)
|
||||
|
||||
def test_draw_detected_charuco(self):
|
||||
detected_points = [[[10, 10], [50, 10], [50, 50], [10, 50]]]
|
||||
img = np.zeros((60, 60), dtype=np.uint8)
|
||||
|
||||
# add extra dimension in Python to create Nx1 Mat with 2 channels
|
||||
points = np.array(detected_points).reshape(-1, 1, 2)
|
||||
img = cv.aruco.drawDetectedCornersCharuco(img, points, cornerColor=255)
|
||||
|
||||
# check that the 4 charuco corners are painted
|
||||
contours, _ = cv.findContours(img, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
|
||||
self.assertEqual(len(contours), 4)
|
||||
for contour in contours:
|
||||
center_x = round(np.average(contour[:, 0, 0]))
|
||||
center_y = round(np.average(contour[:, 0, 1]))
|
||||
center = [center_x, center_y]
|
||||
self.assertTrue(center in detected_points[0])
|
||||
|
||||
# must throw Exception without extra dimension
|
||||
points2 = np.array(detected_points)
|
||||
with self.assertRaises(Exception):
|
||||
img = cv.aruco.drawDetectedCornersCharuco(img, points2, borderColor=255)
|
||||
|
||||
def test_draw_detected_diamonds(self):
|
||||
detected_points = [[[10, 10], [50, 10], [50, 50], [10, 50]]]
|
||||
img = np.zeros((60, 60), dtype=np.uint8)
|
||||
|
||||
# add extra dimension in Python to create Nx4 Mat with 2 channels
|
||||
points = np.array(detected_points).reshape(-1, 4, 1, 2)
|
||||
img = cv.aruco.drawDetectedDiamonds(img, points, borderColor=255)
|
||||
|
||||
# check that the diamonds borders are painted
|
||||
contours, _ = cv.findContours(img, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
|
||||
self.assertEqual(len(contours), 1)
|
||||
self.assertEqual(img[10, 10], 255)
|
||||
self.assertEqual(img[50, 10], 255)
|
||||
self.assertEqual(img[50, 50], 255)
|
||||
self.assertEqual(img[10, 50], 255)
|
||||
|
||||
# must throw Exception without extra dimension
|
||||
points2 = np.array(detected_points)
|
||||
with self.assertRaises(Exception):
|
||||
img = cv.aruco.drawDetectedDiamonds(img, points2, borderColor=255)
|
||||
|
||||
def test_multi_dict_arucodetector(self):
|
||||
aruco_params = cv.aruco.DetectorParameters()
|
||||
aruco_dicts = [
|
||||
cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_250),
|
||||
cv.aruco.getPredefinedDictionary(cv.aruco.DICT_5X5_250)
|
||||
]
|
||||
aruco_detector = cv.aruco.ArucoDetector(aruco_dicts, aruco_params)
|
||||
id = 2
|
||||
marker_size = 100
|
||||
offset = 10
|
||||
img_marker1 = cv.aruco.generateImageMarker(aruco_dicts[0], id, marker_size, aruco_params.markerBorderBits)
|
||||
img_marker1 = np.pad(img_marker1, pad_width=offset, mode='constant', constant_values=255)
|
||||
img_marker2 = cv.aruco.generateImageMarker(aruco_dicts[1], id, marker_size, aruco_params.markerBorderBits)
|
||||
img_marker2 = np.pad(img_marker2, pad_width=offset, mode='constant', constant_values=255)
|
||||
img_markers = np.concatenate((img_marker1, img_marker2), axis=1)
|
||||
|
||||
corners, ids, rejected, dictIndices = aruco_detector.detectMarkersMultiDict(img_markers)
|
||||
|
||||
self.assertEqual(2, len(ids))
|
||||
self.assertEqual(id, ids[0])
|
||||
self.assertEqual(id, ids[1])
|
||||
self.assertEqual(2, len(dictIndices))
|
||||
self.assertEqual(0, dictIndices[0])
|
||||
self.assertEqual(1, dictIndices[1])
|
||||
|
||||
if __name__ == '__main__':
|
||||
NewOpenCVTests.bootstrap()
|
||||
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
'''
|
||||
example to detect upright people in images using HOG features
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
|
||||
def inside(r, q):
|
||||
rx, ry, rw, rh = r
|
||||
qx, qy, qw, qh = q
|
||||
return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh
|
||||
|
||||
from tests_common import NewOpenCVTests, intersectionRate
|
||||
|
||||
class peopledetect_test(NewOpenCVTests):
|
||||
def test_peopledetect(self):
|
||||
|
||||
hog = cv.HOGDescriptor()
|
||||
hog.setSVMDetector( cv.HOGDescriptor_getDefaultPeopleDetector() )
|
||||
|
||||
dirPath = 'samples/data/'
|
||||
samples = ['basketball1.png', 'basketball2.png']
|
||||
|
||||
testPeople = [
|
||||
[[23, 76, 164, 477], [440, 22, 637, 478]],
|
||||
[[23, 76, 164, 477], [440, 22, 637, 478]]
|
||||
]
|
||||
|
||||
eps = 0.5
|
||||
|
||||
for sample in samples:
|
||||
|
||||
img = self.get_sample(dirPath + sample, 0)
|
||||
|
||||
found, _w = hog.detectMultiScale(img, winStride=(8,8), padding=(32,32), scale=1.05)
|
||||
found_filtered = []
|
||||
for ri, r in enumerate(found):
|
||||
for qi, q in enumerate(found):
|
||||
if ri != qi and inside(r, q):
|
||||
break
|
||||
else:
|
||||
found_filtered.append(r)
|
||||
|
||||
matches = 0
|
||||
|
||||
for i in range(len(found_filtered)):
|
||||
for j in range(len(testPeople)):
|
||||
|
||||
found_rect = (found_filtered[i][0], found_filtered[i][1],
|
||||
found_filtered[i][0] + found_filtered[i][2],
|
||||
found_filtered[i][1] + found_filtered[i][3])
|
||||
|
||||
if intersectionRate(found_rect, testPeople[j][0]) > eps or intersectionRate(found_rect, testPeople[j][1]) > eps:
|
||||
matches += 1
|
||||
|
||||
self.assertGreater(matches, 0)
|
||||
|
||||
if __name__ == '__main__':
|
||||
NewOpenCVTests.bootstrap()
|
||||
@@ -0,0 +1,86 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#!/usr/bin/env python
|
||||
'''
|
||||
===============================================================================
|
||||
QR code detect and decode pipeline.
|
||||
===============================================================================
|
||||
'''
|
||||
import os
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
from tests_common import NewOpenCVTests, unittest
|
||||
|
||||
class qrcode_detector_test(NewOpenCVTests):
|
||||
|
||||
def test_detect(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/link_ocv.jpg'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.QRCodeDetector()
|
||||
retval, points = detector.detect(img)
|
||||
self.assertTrue(retval)
|
||||
self.assertEqual(points.shape, (1, 4, 2))
|
||||
|
||||
def test_detect_and_decode(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/link_ocv.jpg'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.QRCodeDetector()
|
||||
retval, points, straight_qrcode = detector.detectAndDecode(img)
|
||||
self.assertEqual(retval, "https://opencv.org/")
|
||||
self.assertEqual(points.shape, (1, 4, 2))
|
||||
|
||||
def test_detect_multi(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/multiple/6_qrcodes.png'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.QRCodeDetector()
|
||||
retval, points = detector.detectMulti(img)
|
||||
self.assertTrue(retval)
|
||||
self.assertEqual(points.shape, (6, 4, 2))
|
||||
|
||||
def test_detect_and_decode_multi(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/multiple/6_qrcodes.png'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.QRCodeDetector()
|
||||
retval, decoded_data, points, straight_qrcode = detector.detectAndDecodeMulti(img)
|
||||
self.assertTrue(retval)
|
||||
self.assertEqual(len(decoded_data), 6)
|
||||
self.assertTrue("TWO STEPS FORWARD" in decoded_data)
|
||||
self.assertTrue("EXTRA" in decoded_data)
|
||||
self.assertTrue("SKIP" in decoded_data)
|
||||
self.assertTrue("STEP FORWARD" in decoded_data)
|
||||
self.assertTrue("STEP BACK" in decoded_data)
|
||||
self.assertTrue("QUESTION" in decoded_data)
|
||||
self.assertEqual(points.shape, (6, 4, 2))
|
||||
|
||||
def test_decode_non_ascii(self):
|
||||
import sys
|
||||
if sys.version_info[0] < 3:
|
||||
raise unittest.SkipTest('Python 2.x is not supported')
|
||||
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/umlaut.png'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.QRCodeDetector()
|
||||
decoded_data, _, _ = detector.detectAndDecode(img)
|
||||
self.assertTrue(isinstance(decoded_data, str))
|
||||
self.assertTrue("Müllheimstrasse" in decoded_data)
|
||||
|
||||
def test_kanji(self):
|
||||
inp = "こんにちは世界"
|
||||
inp_bytes = inp.encode("shift-jis")
|
||||
|
||||
params = cv.QRCodeEncoder_Params()
|
||||
params.mode = cv.QRCodeEncoder_MODE_KANJI
|
||||
encoder = cv.QRCodeEncoder_create(params)
|
||||
qrcode = encoder.encode(inp_bytes)
|
||||
qrcode = cv.resize(qrcode, (0, 0), fx=2, fy=2, interpolation=cv.INTER_NEAREST)
|
||||
|
||||
detector = cv.QRCodeDetector()
|
||||
data, _, _ = detector.detectAndDecodeBytes(qrcode)
|
||||
self.assertEqual(data, inp_bytes)
|
||||
self.assertEqual(detector.getEncoding(), cv.QRCodeEncoder_ECI_SHIFT_JIS)
|
||||
self.assertEqual(data.decode("shift-jis"), inp)
|
||||
|
||||
_, data, _, _ = detector.detectAndDecodeBytesMulti(qrcode)
|
||||
self.assertEqual(data[0], inp_bytes)
|
||||
self.assertEqual(detector.getEncoding(0), cv.QRCodeEncoder_ECI_SHIFT_JIS)
|
||||
self.assertEqual(data[0].decode("shift-jis"), inp)
|
||||
Reference in New Issue
Block a user