Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/3.4' into merge-3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
asmorkalov committed Jun 20, 2023
2 parents f2f0025 + ddcbd2c commit eeebdf0
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 23 deletions.
5 changes: 3 additions & 2 deletions apps/createsamples/utility.cpp
Expand Up @@ -70,7 +70,7 @@ using namespace cv;

static int icvMkDir( const char* filename )
{
char path[PATH_MAX];
char path[PATH_MAX+1];
char* p;
int pos;

Expand All @@ -83,7 +83,8 @@ static int icvMkDir( const char* filename )
mode = 0755;
#endif /* _WIN32 */

strcpy( path, filename );
path[0] = '\0';
strncat( path, filename, PATH_MAX );

p = path;
for( ; ; )
Expand Down
7 changes: 6 additions & 1 deletion modules/core/include/opencv2/core/optim.hpp
Expand Up @@ -256,6 +256,7 @@ class CV_EXPORTS ConjGradSolver : public MinProblemSolver
//! return codes for cv::solveLP() function
enum SolveLPResult
{
SOLVELP_LOST = -3, //!< problem is feasible, but solver lost solution due to floating-point arithmetic errors
SOLVELP_UNBOUNDED = -2, //!< problem is unbounded (target function can achieve arbitrary high values)
SOLVELP_UNFEASIBLE = -1, //!< problem is unfeasible (there are no points that satisfy all the constraints imposed)
SOLVELP_SINGLE = 0, //!< there is only one maximum for target function
Expand Down Expand Up @@ -291,9 +292,13 @@ in the latter case it is understood to correspond to \f$c^T\f$.
and the remaining to \f$A\f$. It should contain 32- or 64-bit floating point numbers.
@param z The solution will be returned here as a column-vector - it corresponds to \f$c\f$ in the
formulation above. It will contain 64-bit floating point numbers.
@param constr_eps allowed numeric disparity for constraints
@return One of cv::SolveLPResult
*/
CV_EXPORTS_W int solveLP(InputArray Func, InputArray Constr, OutputArray z);
CV_EXPORTS_W int solveLP(InputArray Func, InputArray Constr, OutputArray z, double constr_eps);

/** @overload */
CV_EXPORTS int solveLP(InputArray Func, InputArray Constr, OutputArray z);

//! @}

Expand Down
20 changes: 18 additions & 2 deletions modules/core/src/lpsolver.cpp
Expand Up @@ -90,7 +90,7 @@ static void swap_columns(Mat_<double>& A,int col1,int col2);
#define SWAP(type,a,b) {type tmp=(a);(a)=(b);(b)=tmp;}

//return codes:-2 (no_sol - unbdd),-1(no_sol - unfsbl), 0(single_sol), 1(multiple_sol=>least_l2_norm)
int solveLP(InputArray Func_, InputArray Constr_, OutputArray z_)
int solveLP(InputArray Func_, InputArray Constr_, OutputArray z_, double constr_eps)
{
dprintf(("call to solveLP\n"));

Expand Down Expand Up @@ -143,9 +143,25 @@ int solveLP(InputArray Func_, InputArray Constr_, OutputArray z_)
}

z.copyTo(z_);

//check constraints feasibility
Mat prod = Constr(Rect(0, 0, Constr.cols - 1, Constr.rows)) * z;
Mat constr_check = Constr.col(Constr.cols - 1) - prod;
double min_value = 0.0;
minMaxIdx(constr_check, &min_value);
if (min_value < -constr_eps)
{
return SOLVELP_LOST;
}

return res;
}

int solveLP(InputArray Func, InputArray Constr, OutputArray z)
{
return solveLP(Func, Constr, z, 1e-12);
}

static int initialize_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow){
N.resize(c.cols);
N[0]=0;
Expand Down Expand Up @@ -255,7 +271,7 @@ static int initialize_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<
static int inner_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow){

for(;;){
static MatIterator_<double> pos_ptr;
MatIterator_<double> pos_ptr;
int e=-1,pos_ctr=0,min_var=INT_MAX;
bool all_nonzero=true;
for(pos_ptr=c.begin();pos_ptr!=c.end();pos_ptr++,pos_ctr++){
Expand Down
14 changes: 14 additions & 0 deletions modules/core/test/test_lpsolver.cpp
Expand Up @@ -151,4 +151,18 @@ TEST(Core_LPSolver, issue_12337)
EXPECT_ANY_THROW(Mat1b z_8u; cv::solveLP(A, B, z_8u));
}

// NOTE: Test parameters found experimentally to get numerically inaccurate result.
// The test behaviour may change after algorithm tuning and may removed.
TEST(Core_LPSolver, issue_12343)
{
Mat A = (cv::Mat_<double>(4, 1) << 3., 3., 3., 4.);
Mat B = (cv::Mat_<double>(4, 5) << 0., 1., 4., 4., 3.,
3., 1., 2., 2., 3.,
4., 4., 0., 1., 4.,
4., 0., 4., 1., 4.);
Mat z;
int result = cv::solveLP(A, B, z);
EXPECT_EQ(SOLVELP_LOST, result);
}

}} // namespace
31 changes: 17 additions & 14 deletions modules/imgproc/src/distransform.cpp
Expand Up @@ -448,7 +448,7 @@ static void getDistanceTransformMask( int maskType, float *metrics )

struct DTColumnInvoker : ParallelLoopBody
{
DTColumnInvoker( const Mat* _src, Mat* _dst, const int* _sat_tab, const float* _sqr_tab)
DTColumnInvoker( const Mat* _src, Mat* _dst, const int* _sat_tab, const int* _sqr_tab)
{
src = _src;
dst = _dst;
Expand Down Expand Up @@ -481,20 +481,20 @@ struct DTColumnInvoker : ParallelLoopBody
{
dist = dist + 1 - sat_tab[dist - d[j]];
d[j] = dist;
dptr[0] = sqr_tab[dist];
dptr[0] = (float)sqr_tab[dist];
}
}
}

const Mat* src;
Mat* dst;
const int* sat_tab;
const float* sqr_tab;
const int* sqr_tab;
};

struct DTRowInvoker : ParallelLoopBody
{
DTRowInvoker( Mat* _dst, const float* _sqr_tab, const float* _inv_tab )
DTRowInvoker( Mat* _dst, const int* _sqr_tab, const float* _inv_tab )
{
dst = _dst;
sqr_tab = _sqr_tab;
Expand Down Expand Up @@ -529,7 +529,7 @@ struct DTRowInvoker : ParallelLoopBody
for(;;k--)
{
p = v[k];
float s = (fq + sqr_tab[q] - d[p] - sqr_tab[p])*inv_tab[q - p];
float s = (fq - d[p] + (sqr_tab[q]-sqr_tab[p]))*inv_tab[q - p];
if( s > z[k] )
{
k++;
Expand All @@ -552,28 +552,28 @@ struct DTRowInvoker : ParallelLoopBody
}

Mat* dst;
const float* sqr_tab;
const int* sqr_tab;
const float* inv_tab;
};

static void
trueDistTrans( const Mat& src, Mat& dst )
{
const float inf = 1e15f;
const int inf = INT_MAX;

CV_Assert( src.size() == dst.size() );

CV_Assert( src.type() == CV_8UC1 && dst.type() == CV_32FC1 );
int i, m = src.rows, n = src.cols;

cv::AutoBuffer<uchar> _buf(std::max(m*2*sizeof(float) + (m*3+1)*sizeof(int), n*2*sizeof(float)));
cv::AutoBuffer<uchar> _buf(std::max(m*2*sizeof(int) + (m*3+1)*sizeof(int), n*2*sizeof(float)));
// stage 1: compute 1d distance transform of each column
float* sqr_tab = (float*)_buf.data();
int* sqr_tab = (int*)_buf.data();
int* sat_tab = cv::alignPtr((int*)(sqr_tab + m*2), sizeof(int));
int shift = m*2;

for( i = 0; i < m; i++ )
sqr_tab[i] = (float)(i*i);
sqr_tab[i] = i*i;
for( i = m; i < m*2; i++ )
sqr_tab[i] = inf;
for( i = 0; i < shift; i++ )
Expand All @@ -584,13 +584,14 @@ trueDistTrans( const Mat& src, Mat& dst )
cv::parallel_for_(cv::Range(0, n), cv::DTColumnInvoker(&src, &dst, sat_tab, sqr_tab), src.total()/(double)(1<<16));

// stage 2: compute modified distance transform for each row
float* inv_tab = sqr_tab + n;
float* inv_tab = (float*)sqr_tab + n;

inv_tab[0] = sqr_tab[0] = 0.f;
inv_tab[0] = 0.f;
sqr_tab[0] = 0;
for( i = 1; i < n; i++ )
{
inv_tab[i] = (float)(0.5/i);
sqr_tab[i] = (float)(i*i);
sqr_tab[i] = i*i;
}

cv::parallel_for_(cv::Range(0, m), cv::DTRowInvoker(&dst, sqr_tab, inv_tab));
Expand Down Expand Up @@ -750,7 +751,9 @@ void cv::distanceTransform( InputArray _src, OutputArray _dst, OutputArray _labe
CV_IPP_CHECK()
{
#if IPP_DISABLE_PERF_TRUE_DIST_MT
if(cv::getNumThreads()<=1 || (src.total()<(int)(1<<14)))
// IPP uses floats, but 4097 cannot be squared into a float
if((cv::getNumThreads()<=1 || (src.total()<(int)(1<<14))) &&
src.rows < 4097 && src.cols < 4097)
#endif
{
IppStatus status;
Expand Down
42 changes: 42 additions & 0 deletions modules/imgproc/test/test_distancetransform.cpp
Expand Up @@ -302,4 +302,46 @@ BIGDATA_TEST(Imgproc_DistanceTransform, large_image_12218)
EXPECT_EQ(nz, (size.height*size.width / 2));
}

TEST(Imgproc_DistanceTransform, wide_image_22732)
{
Mat src = Mat::zeros(1, 4099, CV_8U); // 4099 or larger used to be bad
Mat dist(src.rows, src.cols, CV_32F);
distanceTransform(src, dist, DIST_L2, DIST_MASK_PRECISE, CV_32F);
int nz = countNonZero(dist);
EXPECT_EQ(nz, 0);
}

TEST(Imgproc_DistanceTransform, large_square_22732)
{
Mat src = Mat::zeros(8000, 8005, CV_8U), dist;
distanceTransform(src, dist, DIST_L2, DIST_MASK_PRECISE, CV_32F);
int nz = countNonZero(dist);
EXPECT_EQ(dist.size(), src.size());
EXPECT_EQ(dist.type(), CV_32F);
EXPECT_EQ(nz, 0);

Point p0(src.cols-1, src.rows-1);
src.setTo(1);
src.at<uchar>(p0) = 0;
distanceTransform(src, dist, DIST_L2, DIST_MASK_PRECISE, CV_32F);
EXPECT_EQ(dist.size(), src.size());
EXPECT_EQ(dist.type(), CV_32F);
bool first = true;
int nerrs = 0;
for (int y = 0; y < dist.rows; y++)
for (int x = 0; x < dist.cols; x++) {
float d = dist.at<float>(y, x);
double dx = (double)(x - p0.x), dy = (double)(y - p0.y);
float d0 = (float)sqrt(dx*dx + dy*dy);
if (std::abs(d0 - d) > 1) {
if (first) {
printf("y=%d, x=%d. dist_ref=%.2f, dist=%.2f\n", y, x, d0, d);
first = false;
}
nerrs++;
}
}
EXPECT_EQ(0, nerrs) << "reference distance map is different from computed one at " << nerrs << " pixels\n";
}

}} // namespace
2 changes: 1 addition & 1 deletion modules/videoio/src/cap_ffmpeg_impl.hpp
Expand Up @@ -1847,7 +1847,7 @@ int64_t CvCapture_FFMPEG::get_bitrate() const

double CvCapture_FFMPEG::get_fps() const
{
#if 0 && LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(55, 1, 100) && LIBAVFORMAT_VERSION_MICRO >= 100
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(55, 1, 100) && LIBAVFORMAT_VERSION_MICRO >= 100
double fps = r2d(av_guess_frame_rate(ic, ic->streams[video_stream], NULL));
#else
double fps = r2d(ic->streams[video_stream]->avg_frame_rate);
Expand Down
17 changes: 14 additions & 3 deletions platforms/ios/cmake/Toolchains/common-ios-toolchain.cmake
Expand Up @@ -101,8 +101,17 @@ if(NOT DEFINED IPHONEOS_DEPLOYMENT_TARGET AND NOT MAC_CATALYST)
endif()

if(NOT __IN_TRY_COMPILE)
set(_xcodebuild_wrapper "${CMAKE_BINARY_DIR}/xcodebuild_wrapper")
if(NOT EXISTS "${_xcodebuild_wrapper}")
set(_xcodebuild_wrapper "")
if(NOT (CMAKE_VERSION VERSION_LESS "3.25.1")) # >= 3.25.1
# no workaround is required (#23156)
elseif(NOT (CMAKE_VERSION VERSION_LESS "3.25.0")) # >= 3.25.0 < 3.25.1
if(NOT OPENCV_SKIP_MESSAGE_ISSUE_23156)
message(FATAL_ERROR "OpenCV: Please upgrade CMake to 3.25.1+. Current CMake version is ${CMAKE_VERSION}. Details: https://github.com/opencv/opencv/issues/23156")
endif()
else() # < 3.25.0, apply workaround from #13912
set(_xcodebuild_wrapper "${CMAKE_BINARY_DIR}/xcodebuild_wrapper")
endif()
if(_xcodebuild_wrapper AND NOT EXISTS "${_xcodebuild_wrapper}")
set(_xcodebuild_wrapper_tmp "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/xcodebuild_wrapper")
if(NOT DEFINED CMAKE_MAKE_PROGRAM) # empty since CMake 3.10
find_program(XCODEBUILD_PATH "xcodebuild")
Expand All @@ -122,7 +131,9 @@ if(NOT __IN_TRY_COMPILE)
configure_file("${CMAKE_CURRENT_LIST_DIR}/xcodebuild_wrapper.in" "${_xcodebuild_wrapper_tmp}" @ONLY)
file(COPY "${_xcodebuild_wrapper_tmp}" DESTINATION ${CMAKE_BINARY_DIR} FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
endif()
set(CMAKE_MAKE_PROGRAM "${_xcodebuild_wrapper}" CACHE INTERNAL "" FORCE)
if(_xcodebuild_wrapper)
set(CMAKE_MAKE_PROGRAM "${_xcodebuild_wrapper}" CACHE INTERNAL "" FORCE)
endif()
endif()

# Standard settings
Expand Down

0 comments on commit eeebdf0

Please sign in to comment.