1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 00:03:03 +04:00

Merge remote-tracking branch 'upstream/3.4' into merge-3.4

This commit is contained in:
Alexander Alekhin
2018-10-26 17:56:28 +03:00
40 changed files with 647 additions and 322 deletions
+1 -1
View File
@@ -4,5 +4,5 @@ ocv_define_module(objdetect opencv_core opencv_imgproc opencv_calib3d WRAP java
if(HAVE_QUIRC)
get_property(QUIRC_INCLUDE GLOBAL PROPERTY QUIRC_INCLUDE_DIR)
ocv_include_directories(${QUIRC_INCLUDE})
ocv_target_link_libraries(${PROJECT_NAME} quirc)
ocv_target_link_libraries(${the_module} quirc)
endif()
@@ -0,0 +1,106 @@
// 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.
#include "perf_precomp.hpp"
namespace opencv_test
{
namespace
{
typedef ::perf::TestBaseWithParam< std::string > Perf_Objdetect_QRCode;
PERF_TEST_P_(Perf_Objdetect_QRCode, detect)
{
const std::string name_current_image = GetParam();
const std::string root = "cv/qrcode/";
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
std::vector< Point > corners;
TEST_CYCLE() ASSERT_TRUE(detectQRCode(src, corners));
SANITY_CHECK(corners);
}
#ifdef HAVE_QUIRC
PERF_TEST_P_(Perf_Objdetect_QRCode, decode)
{
const std::string name_current_image = GetParam();
const std::string root = "cv/qrcode/";
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
std::vector< Point > corners;
std::string decoded_info;
ASSERT_TRUE(detectQRCode(src, corners));
TEST_CYCLE() ASSERT_TRUE(decodeQRCode(src, corners, decoded_info, straight_barcode));
std::vector<uint8_t> decoded_info_uint8_t(decoded_info.begin(), decoded_info.end());
SANITY_CHECK(decoded_info_uint8_t);
SANITY_CHECK(straight_barcode);
}
#endif
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode,
::testing::Values(
"version_1_down.jpg", "version_1_left.jpg", "version_1_right.jpg", "version_1_up.jpg", "version_1_top.jpg",
"version_5_down.jpg", "version_5_left.jpg", "version_5_right.jpg", "version_5_up.jpg", "version_5_top.jpg",
"russian.jpg", "kanji.jpg", "link_github_ocv.jpg", "link_ocv.jpg", "link_wiki_cv.jpg"
)
);
typedef ::perf::TestBaseWithParam< tuple< std::string, Size > > Perf_Objdetect_Not_QRCode;
PERF_TEST_P_(Perf_Objdetect_Not_QRCode, detect)
{
std::vector<Point> corners;
std::string type_gen = get<0>(GetParam());
Size resolution = get<1>(GetParam());
Mat not_qr_code(resolution, CV_8UC1, Scalar(0));
if (type_gen == "random")
{
RNG rng;
rng.fill(not_qr_code, RNG::UNIFORM, Scalar(0), Scalar(1));
}
TEST_CYCLE() ASSERT_FALSE(detectQRCode(not_qr_code, corners));
SANITY_CHECK_NOTHING();
}
#ifdef HAVE_QUIRC
PERF_TEST_P_(Perf_Objdetect_Not_QRCode, decode)
{
Mat straight_barcode;
std::string decoded_info;
std::vector< Point > corners;
corners.push_back(Point( 0, 0)); corners.push_back(Point( 0, 5));
corners.push_back(Point(10, 0)); corners.push_back(Point(15, 15));
std::string type_gen = get<0>(GetParam());
Size resolution = get<1>(GetParam());
Mat not_qr_code(resolution, CV_8UC1, Scalar(0));
if (type_gen == "random")
{
RNG rng;
rng.fill(not_qr_code, RNG::UNIFORM, Scalar(0), Scalar(1));
}
TEST_CYCLE() ASSERT_FALSE(decodeQRCode(not_qr_code, corners, decoded_info, straight_barcode));
SANITY_CHECK_NOTHING();
}
#endif
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_Not_QRCode,
::testing::Combine(
::testing::Values("zero", "random"),
::testing::Values(Size(640, 480), Size(1280, 720), Size(1920, 1080))
));
}
} // namespace
+47 -212
View File
@@ -94,7 +94,6 @@ typedef struct CvHidHaarClassifierCascade
sqsumtype *pq0, *pq1, *pq2, *pq3;
sumtype *p0, *p1, *p2, *p3;
void** ipp_stages;
bool is_tree;
bool isStumpBased;
} CvHidHaarClassifierCascade;
@@ -128,23 +127,6 @@ icvReleaseHidHaarClassifierCascade( CvHidHaarClassifierCascade** _cascade )
{
if( _cascade && *_cascade )
{
#ifdef HAVE_IPP
CvHidHaarClassifierCascade* cascade = *_cascade;
if( CV_IPP_CHECK_COND && cascade->ipp_stages )
{
int i;
for( i = 0; i < cascade->count; i++ )
{
if( cascade->ipp_stages[i] )
#if IPP_VERSION_X100 < 900 && !IPP_DISABLE_HAAR
ippiHaarClassifierFree_32f( (IppiHaarClassifier_32f*)cascade->ipp_stages[i] );
#else
cvFree(&cascade->ipp_stages[i]);
#endif
}
}
cvFree( &cascade->ipp_stages );
#endif
cvFree( _cascade );
}
}
@@ -153,10 +135,6 @@ icvReleaseHidHaarClassifierCascade( CvHidHaarClassifierCascade** _cascade )
static CvHidHaarClassifierCascade*
icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
{
CvRect* ipp_features = 0;
float *ipp_weights = 0, *ipp_thresholds = 0, *ipp_val1 = 0, *ipp_val2 = 0;
int* ipp_counts = 0;
CvHidHaarClassifierCascade* out = 0;
int i, j, k, l;
@@ -312,72 +290,9 @@ icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
}
}
#if defined HAVE_IPP && !IPP_DISABLE_HAAR
int can_use_ipp = CV_IPP_CHECK_COND && (!out->has_tilted_features && !out->is_tree && out->isStumpBased);
if( can_use_ipp )
{
int ipp_datasize = cascade->count*sizeof(out->ipp_stages[0]);
float ipp_weight_scale=(float)(1./((orig_window_size.width-icv_object_win_border*2)*
(orig_window_size.height-icv_object_win_border*2)));
out->ipp_stages = (void**)cvAlloc( ipp_datasize );
memset( out->ipp_stages, 0, ipp_datasize );
ipp_features = (CvRect*)cvAlloc( max_count*3*sizeof(ipp_features[0]) );
ipp_weights = (float*)cvAlloc( max_count*3*sizeof(ipp_weights[0]) );
ipp_thresholds = (float*)cvAlloc( max_count*sizeof(ipp_thresholds[0]) );
ipp_val1 = (float*)cvAlloc( max_count*sizeof(ipp_val1[0]) );
ipp_val2 = (float*)cvAlloc( max_count*sizeof(ipp_val2[0]) );
ipp_counts = (int*)cvAlloc( max_count*sizeof(ipp_counts[0]) );
for( i = 0; i < cascade->count; i++ )
{
CvHaarStageClassifier* stage_classifier = cascade->stage_classifier + i;
for( j = 0, k = 0; j < stage_classifier->count; j++ )
{
CvHaarClassifier* classifier = stage_classifier->classifier + j;
int rect_count = 2 + (classifier->haar_feature->rect[2].r.width != 0);
ipp_thresholds[j] = classifier->threshold[0];
ipp_val1[j] = classifier->alpha[0];
ipp_val2[j] = classifier->alpha[1];
ipp_counts[j] = rect_count;
for( l = 0; l < rect_count; l++, k++ )
{
ipp_features[k] = classifier->haar_feature->rect[l].r;
//ipp_features[k].y = orig_window_size.height - ipp_features[k].y - ipp_features[k].height;
ipp_weights[k] = classifier->haar_feature->rect[l].weight*ipp_weight_scale;
}
}
if( ippiHaarClassifierInitAlloc_32f( (IppiHaarClassifier_32f**)&out->ipp_stages[i],
(const IppiRect*)ipp_features, ipp_weights, ipp_thresholds,
ipp_val1, ipp_val2, ipp_counts, stage_classifier->count ) < 0 )
break;
}
if( i < cascade->count )
{
for( j = 0; j < i; j++ )
if( out->ipp_stages[i] )
ippiHaarClassifierFree_32f( (IppiHaarClassifier_32f*)out->ipp_stages[i] );
cvFree( &out->ipp_stages );
}
}
#endif
cascade->hid_cascade = out;
assert( (char*)haar_node_ptr - (char*)out <= datasize );
cvFree( &ipp_features );
cvFree( &ipp_weights );
cvFree( &ipp_thresholds );
cvFree( &ipp_val1 );
cvFree( &ipp_val2 );
cvFree( &ipp_counts );
return out;
}
@@ -975,120 +890,54 @@ public:
std::vector<int> rejectLevelsLocal;
std::vector<double> levelWeightsLocal;
#ifdef HAVE_IPP
if(CV_IPP_CHECK_COND && cascade->hid_cascade->ipp_stages )
{
IppiRect iequRect = {equRect.x, equRect.y, equRect.width, equRect.height};
CV_INSTRUMENT_FUN_IPP(ippiRectStdDev_32f_C1R, sum1.ptr<float>(y1), (int)sum1.step,
sqsum1.ptr<double>(y1), (int)sqsum1.step,
norm1->ptr<float>(y1), (int)norm1->step,
ippiSize(ssz.width, ssz.height), iequRect);
int positive = (ssz.width/ystep)*((ssz.height + ystep-1)/ystep);
if( ystep == 1 )
(*mask1) = Scalar::all(1);
else
for( y = y1; y < y2; y++ )
{
uchar* mask1row = mask1->ptr(y);
memset( mask1row, 0, ssz.width );
if( y % ystep == 0 )
for( x = 0; x < ssz.width; x += ystep )
mask1row[x] = (uchar)1;
}
for( int j = 0; j < cascade->count; j++ )
for( y = y1; y < y2; y += ystep )
for( x = 0; x < ssz.width; x += ystep )
{
if (CV_INSTRUMENT_FUN_IPP(ippiApplyHaarClassifier_32f_C1R,
sum1.ptr<float>(y1), (int)sum1.step,
norm1->ptr<float>(y1), (int)norm1->step,
mask1->ptr<uchar>(y1), (int)mask1->step,
ippiSize(ssz.width, ssz.height), &positive,
cascade->hid_cascade->stage_classifier[j].threshold,
(IppiHaarClassifier_32f*)cascade->hid_cascade->ipp_stages[j]) < 0 )
positive = 0;
if( positive <= 0 )
break;
double gypWeight;
int result = cvRunHaarClassifierCascadeSum( cascade, cvPoint(x,y), gypWeight, 0 );
if( rejectLevels )
{
if( result == 1 )
result = -1*cascade->count;
if( cascade->count + result < 4 )
{
vecLocal.push_back(Rect(cvRound(x*factor), cvRound(y*factor),
winSize.width, winSize.height));
rejectLevelsLocal.push_back(-result);
levelWeightsLocal.push_back(gypWeight);
if (vecLocal.size() >= PARALLEL_LOOP_BATCH_SIZE)
{
mtx->lock();
vec->insert(vec->end(), vecLocal.begin(), vecLocal.end());
rejectLevels->insert(rejectLevels->end(), rejectLevelsLocal.begin(), rejectLevelsLocal.end());
levelWeights->insert(levelWeights->end(), levelWeightsLocal.begin(), levelWeightsLocal.end());
mtx->unlock();
vecLocal.clear();
rejectLevelsLocal.clear();
levelWeightsLocal.clear();
}
}
}
else
{
if( result > 0 )
{
vecLocal.push_back(Rect(cvRound(x*factor), cvRound(y*factor),
winSize.width, winSize.height));
if (vecLocal.size() >= PARALLEL_LOOP_BATCH_SIZE)
{
mtx->lock();
vec->insert(vec->end(), vecLocal.begin(), vecLocal.end());
mtx->unlock();
vecLocal.clear();
}
}
}
}
CV_IMPL_ADD(CV_IMPL_IPP|CV_IMPL_MT);
if( positive > 0 )
for( y = y1; y < y2; y += ystep )
{
uchar* mask1row = mask1->ptr(y);
for( x = 0; x < ssz.width; x += ystep )
if( mask1row[x] != 0 )
{
vecLocal.push_back(Rect(cvRound(x*factor), cvRound(y*factor),
winSize.width, winSize.height));
if (vecLocal.size() >= PARALLEL_LOOP_BATCH_SIZE)
{
mtx->lock();
vec->insert(vec->end(), vecLocal.begin(), vecLocal.end());
mtx->unlock();
vecLocal.clear();
}
if( --positive == 0 )
break;
}
if( positive == 0 )
break;
}
}
else
#endif // IPP
for( y = y1; y < y2; y += ystep )
for( x = 0; x < ssz.width; x += ystep )
{
double gypWeight;
int result = cvRunHaarClassifierCascadeSum( cascade, cvPoint(x,y), gypWeight, 0 );
if( rejectLevels )
{
if( result == 1 )
result = -1*cascade->count;
if( cascade->count + result < 4 )
{
vecLocal.push_back(Rect(cvRound(x*factor), cvRound(y*factor),
winSize.width, winSize.height));
rejectLevelsLocal.push_back(-result);
levelWeightsLocal.push_back(gypWeight);
if (vecLocal.size() >= PARALLEL_LOOP_BATCH_SIZE)
{
mtx->lock();
vec->insert(vec->end(), vecLocal.begin(), vecLocal.end());
rejectLevels->insert(rejectLevels->end(), rejectLevelsLocal.begin(), rejectLevelsLocal.end());
levelWeights->insert(levelWeights->end(), levelWeightsLocal.begin(), levelWeightsLocal.end());
mtx->unlock();
vecLocal.clear();
rejectLevelsLocal.clear();
levelWeightsLocal.clear();
}
}
}
else
{
if( result > 0 )
{
vecLocal.push_back(Rect(cvRound(x*factor), cvRound(y*factor),
winSize.width, winSize.height));
if (vecLocal.size() >= PARALLEL_LOOP_BATCH_SIZE)
{
mtx->lock();
vec->insert(vec->end(), vecLocal.begin(), vecLocal.end());
mtx->unlock();
vecLocal.clear();
}
}
}
}
if (rejectLevelsLocal.size())
{
@@ -1283,12 +1132,6 @@ cvHaarDetectObjectsForROC( const CvArr* _img,
if( flags & CV_HAAR_SCALE_IMAGE )
{
CvSize winSize0 = cascade->orig_window_size;
#ifdef HAVE_IPP
int use_ipp = CV_IPP_CHECK_COND && (cascade->hid_cascade->ipp_stages != 0);
if( use_ipp )
normImg.reset(cvCreateMat( img->rows, img->cols, CV_32FC1));
#endif
imgSmall.reset(cvCreateMat( img->rows + 1, img->cols + 1, CV_8UC1 ));
for( factor = 1; ; factor *= scaleFactor )
@@ -1330,15 +1173,7 @@ cvHaarDetectObjectsForROC( const CvArr* _img,
int stripCount = ((sz1.width/ystep)*(sz1.height + ystep-1)/ystep + LOCS_PER_THREAD/2)/LOCS_PER_THREAD;
stripCount = std::min(std::max(stripCount, 1), 100);
#ifdef HAVE_IPP
if( use_ipp )
{
cv::Mat fsum(sum1.rows, sum1.cols, CV_32F, sum1.data.ptr, sum1.step);
cv::cvarrToMat(&sum1).convertTo(fsum, CV_32F, 1, -(1<<24));
}
else
#endif
cvSetImagesForHaarClassifierCascade( cascade, &sum1, &sqsum1, _tilted, 1. );
cvSetImagesForHaarClassifierCascade( cascade, &sum1, &sqsum1, _tilted, 1. );
cv::Mat _norm1 = cv::cvarrToMat(&norm1), _mask1 = cv::cvarrToMat(&mask1);
cv::parallel_for_(cv::Range(0, stripCount),
+19 -4
View File
@@ -865,6 +865,20 @@ bool QRDecode::updatePerspective()
return true;
}
inline Point computeOffset(const vector<Point>& v)
{
// compute the width/height of convex hull
Rect areaBox = boundingRect(v);
// compute the good offset
// the box is consisted by 7 steps
// to pick the middle of the stripe, it needs to be 1/14 of the size
const int cStep = 7 * 2;
Point offset = Point(areaBox.width, areaBox.height);
offset /= cStep;
return offset;
}
bool QRDecode::versionDefinition()
{
LineIterator line_iter(intermediate, Point2f(0, 0), Point2f(test_perspective_size, test_perspective_size));
@@ -882,17 +896,18 @@ bool QRDecode::versionDefinition()
Mat mask_roi = mask(Range(1, intermediate.rows - 1), Range(1, intermediate.cols - 1));
findNonZero(mask_roi, non_zero_elem);
convexHull(Mat(non_zero_elem), locations);
Point offset = computeOffset(locations);
Point temp_remote = locations[0], remote_point;
const Point delta_diff = Point(4, 4);
const Point delta_diff = offset;
for (size_t i = 0; i < locations.size(); i++)
{
if (norm(black_point - temp_remote) < norm(black_point - locations[i]))
if (norm(black_point - temp_remote) <= norm(black_point - locations[i]))
{
const uint8_t value = intermediate.at<uint8_t>(temp_remote - delta_diff);
if (value == 0) { remote_point = temp_remote - delta_diff; }
else { remote_point = temp_remote; }
temp_remote = locations[i];
if (value == 0) { remote_point = temp_remote - delta_diff; }
else { remote_point = temp_remote - (delta_diff / 2); }
}
}
+2 -1
View File
@@ -115,7 +115,8 @@ INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode, testing::ValuesIn(qrcode_images_
TEST(Objdetect_QRCode_basic, not_found_qrcode)
{
std::vector<Point> corners, straight_barcode;
std::vector<Point> corners;
Mat straight_barcode;
std::string decoded_info;
Mat zero_image = Mat::zeros(256, 256, CV_8UC1);
EXPECT_FALSE(detectQRCode(zero_image, corners));