Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8354295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:30:23+00:00 2026-06-09T09:30:23+00:00

I have performed Closing morphological operation and I am getting different result with the

  • 0

I have performed Closing morphological operation and I am getting different result with the C and C++ API (OpenCV 2.4.2)

Input:

input

With OpenCV ‘C’:

//Set ROI
//Perform Gaussian smoothing
//Perform Canny edge analysis
cvMorphologyEx( src, dst, temp, Mat(), MORPH_CLOSE, 5 );

RESULT:
http://i47.tinypic.com/33e0yfb.png

With Opencv C++

//Set ROI 
//Perform Gaussian smoothing 
//Perform Canny edge analysis
cv::morphologyEx( src, dst, cv::MORPH_CLOSE, cv::Mat(), cv::Point(-1,-1), 5 );

RESULT:
http://i50.tinypic.com/i5vxjo.png

As you can see, the C++ API yields an output with White/Gray border color. Hence, the results are different for both of these APIs.

I have tried different borderType with the C++ API but it always yields the same result.

How can I get the same output as C API in C++? I need it because it affects the detected contours

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:30:25+00:00Added an answer on June 9, 2026 at 9:30 am

    Thank you everybody for answering this question. I have found my error. I am going to describe it in brief below. Hope it helps others facing this problem.

    1) I had executed the C and C++ commands on a ROI image. Apparently, the way OpenCV ‘C’ and ‘C++’ API treat ROI is different.

    2) In ‘C’, a ROI is treated as a completely different image. Hence, when you execute functions such as cvSmooth, cvDilate, etc, where one needs to mentions border Pixel extrapolation methods, the ‘C’ API does not refer back to the original image for pixels beyond left/right/top/bottom most pixel. It actually interpolates the pixel values according to the method you mentioned.

    3) But in ‘C++’, I have found that it always refers back to the original image for pixels beyond left/right/top/bottom most pixel. Hence, the border pixel extrapolation method mentioned doesn’t affect your output if there are pixels in the original image around your ROI.

    I think it applies the order pixel extrapolation method to the original image instead of the ROI unlike the ‘C’ API. I don’t know if this a bug; I haven’t completely read the OpenCV 2.4.2 C++ API documentation. (Please correct me if I am wrong)

    To claim my support, I have posted input/output images below:

    Output for ‘C’ and C++ API:

    INPUT:

    input
    <— input

    OpenCV ‘C’ API:

    IplImage *src = cvLoadImage("input.png", 0);
    cvSetImageROI( src, cvRect(33,19,250,110)); 
    cvSaveImage( "before_gauss.png", src );
    cvSmooth( src, src, CV_GAUSSIAN );  
    cvSaveImage("after_gauss.png", src);
    IplConvKernel *element = cvCreateStructuringElementEx(3,3,1,1,CV_SHAPE_RECT);
    cvCanny( src, src, 140, 40 );
    cvSaveImage("after_canny.png", src);
    cvDilate( src, src, element, 5);
    cvSaveImage("dilate.png", src);
    

    OUTPUT:

    before_gauss <– before_gauss

    after_gauss
    <— after_gauss

    after_canny
    <— after_canny

    dilate
    <— dilate

    OpenCV ‘C++’ API:

    cv::Mat src = cv::imread("input.png", 0);
    cv::Mat src_ROI = src( cv::Rect(33,19,250,110));
    cv::imwrite( "before_gauss.png", src_ROI );
    cv::GaussianBlur( src_ROI, src_ROI, cv::Size(3,3),0 );
    cv::imwrite( "after_gauss.png", src_ROI ); 
    cv::Mat element = cv::getStructuringElement( cv::MORPH_RECT, cv::Size(3, 3), cv::Point(1,1));
    cv::Canny( src_ROI, src_ROI, 140, 40);
    cv::imwrite( "after_canny.png", src_ROI );
    cv::dilate( src_ROI, src_ROI, element, cv::Point(1,1), 5);
    cv::imwrite( "dilate.png", src_ROI );
    

    OUTPUT:

    before_gauss <– before_gauss

    after_gauss

    ^^^^^ after_gauss (NOTE: the borders are no more completely black, they are grayish)

    after_canny

    ^^^^^ after_canny

    dilate

    ^^^^^ dilate

    SOLUTION:

    Create a separate ROI copy and use it for further analysis;

    src_ROI.copyTo( new_src_ROI ); 
    

    Use new_src_ROI for further analysis.
    If anyone has better solution, please post below

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an operation that will be performed MANY times and so I need
I will have to perform a spelling check-like operation in Python as follows: I
I have to perform async execution, and i have wrote the below code var
I have to perform mapping between ISO 3166-1 alpha-3 and IOC country codes. It
I have to perform a large number of replacements in some documents, and the
I have to perform a SQL query to get the top 10 albums according
I have to perform a select on a table in the following manner: I
I have to perform a query similar to: <?php //....connect to database $old =
I have recently decided to switch all my current plain mysql queries performed with
I have parent process that opens child process . I need to perform some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.