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 8972501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:13:56+00:00 2026-06-15T18:13:56+00:00

Can OpenCV be developed using C++ and C together? Following is the program where

  • 0

Can OpenCV be developed using C++ and C together? Following is the program where i meet this problem.It is coded with C and works well.But if I use

cv::Mat saveImage=cv::imread("D:\\opencvStudy\\opencv_test\\TaskDemo\\TaskDemo\\save.jpg");
cv::imshow("save",saveImage);

to replace

IplImage* saveImge =cvLoadImage("D:\\opencvStudy\\opencv_test\\TaskDemo\\TaskDemo\\save.jpg");
cvShowImage("saveimage",saveImge);

I met this:

Unhandled exception at 0x75709673 in TaskDemo.exe: Microsoft C++ exception: cv::Exception at memory location 0x0039ea0c..

Following is the whole program. Hope anyone could help me .Thanks very much

#include<opencv/cv.h>
#include<opencv/highgui.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/tracking.hpp"


#include <iostream>
#include <ctype.h>

CvPoint pt1 = cvPoint(0,0);
CvPoint pt2 = cvPoint(0,0);
bool is_selecting = false;

void cvMouseCallback(int mouseEvent,int x,int y,int flags,void* param)
{
switch(mouseEvent)
{
case CV_EVENT_LBUTTONDOWN:
    pt1 = cvPoint(x,y);
    pt2 = cvPoint(x,y);
    is_selecting = true;
    break;
case CV_EVENT_MOUSEMOVE:
    if(is_selecting)
        pt2 = cvPoint(x,y);
    break;
case CV_EVENT_LBUTTONUP:
    pt2 = cvPoint(x,y);
    is_selecting = false;
    break;
}
return;
 }

int main(int argc,char* argv[])
{
char img_path[80] = "D:\\opencvStudy\\pic\\boldt.jpg";
char save_path[80] = "save.jpg";
char* window = "img";

IplImage* img = cvLoadImage(img_path);  
IplImage* img_show = cvCloneImage(img);

cvNamedWindow(window,CV_WINDOW_AUTOSIZE);
cvSetMouseCallback(window,cvMouseCallback);

char text[80];
CvFont font;
cvInitFont(&font,CV_FONT_HERSHEY_PLAIN,1.0,1.0);
while(true)
{
    cvCopy(img,img_show);
    cvRectangle(img_show,pt1,pt2,cvScalar(255,255,255));
    sprintf(text,"roi = cvRect(%d,%d,%d,%d)",pt1.x,pt1.y,std::abs(pt2.x-pt1.x),std::abs(pt2.y-pt1.y));
    cvPutText(img_show,text,cvPoint(10,20),&font,cvScalar(0,0,255));

    cvShowImage(window,img_show);
    char key = cvWaitKey(10);
    if (key='r')
    {
        cvSetImageROI(img,cvRect(pt1.x,pt1.y,std::abs(pt2.x-pt1.x),std::abs(pt2.y-pt1.y)));
        cvSaveImage(save_path,img);
        cvResetImageROI(img);
        IplImage* saveImge = cvLoadImage("D:\\opencvStudy\\opencv_test\\TaskDemo\\TaskDemo\\save.jpg");

        cvShowImage("saveimage",saveImge);
        //cv::Mat saveImage=cv::imread("D:\\opencvStudy\\opencv_test\\TaskDemo\\TaskDemo\\save.jpg");
        //cv::imshow("save",saveImage);
    }
    else if(key==27)
        break;
}
cvReleaseImage(&img);
cvReleaseImage(&img_show);
return 0;

}

Update

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
#include <ctype.h>

using namespace std;
using namespace cv;
cv::Point pt1=Point(0,0);
cv::Point pt2=Point(0,0);
bool is_selecting=false;

static void onMouse(int mouseEvent,int x,int y,int flags,void* param)
{
switch(mouseEvent)
{
case CV_EVENT_LBUTTONDOWN://CV_EVENT_LBUTTONDOWN
    pt1 = Point(x,y);
    pt2 = Point(x,y);
    is_selecting = true;
    break;
case CV_EVENT_MOUSEMOVE:
    if(is_selecting)
        pt2 = Point(x,y);
    break;
case CV_EVENT_LBUTTONUP:
    pt2 = Point(x,y);
    is_selecting = false;
    break;
}
}

int main()
{
cv::Mat img=cv::imread("D:\\opencvStudy\\pic\\boldt.jpg");
cv::Mat img_show=img.clone();

cv::namedWindow("Task2",0);
cv::setMouseCallback("Task2",onMouse,0);

char text[80];
while(true)
{
    img.copyTo(img_show);
    cv::rectangle(img_show,pt1,pt2,cv::Scalar(255,255,255));
    sprintf(text,"roi = cvRect(%d,%d,%d,%d)",pt1.x,pt1.y,std::abs(pt2.x-pt1.x),std::abs(pt2.y-pt1.y));
    cv::putText(img_show,text,cvPoint(10,20),10,10,Scalar(0,0,255));
    cv::imshow("Task2",img_show);
    char key=cv::waitKey(10);
    if (key='r')
    {
        cv::Mat save=img(cv::Rect(pt1.x,pt1.y,std::abs(pt2.x-pt1.x),std::abs(pt2.y-pt1.y)));
        cv::imwrite("save.jpg",save);
        cv::Mat saveImage=cv::imread("D:\\opencvStudy\\opencv_test\\TaskDemo\\TaskDemo\\save.jpg");
        cv::imshow("save",saveImage);
    }
    else if(key==27)
        break;
}
return 0;

}

  • 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-15T18:13:57+00:00Added an answer on June 15, 2026 at 6:13 pm

    The problem is with conflicting headers. You should not include old and new at the same time like this:

    #include<opencv/cv.h>
    #include<opencv/highgui.h>
    #include "opencv2/highgui/highgui.hpp"
    

    Instead, include the new headers only, like this:

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    

    The reason for this is that the C++ headers also include the C interface.

    UPDATE:

    Of course if you use cv::Mat then you need to use the new functions for that as well.

    CvCopy becomes cv::copy, there is no cvReleaseImage needed, cvRectangle becomes cv::rectangle, cvSetImageROI is img(rect), cvPoint becomes cv::Point cvRect becomes cv::Rect
    cvCloneImage becomes img.clone() and so on…

    Or you need some conversion from cv::Mat to IplImage to use the old functions. There are some questions on SO dealing with converting back-and-forth.

    UPDATE2:

    It is best to remove all cv:: since you are using using namespace cv.

    Then check for functions and variables left with the old cv prefix, and remove those prefixes. (I’ve still found one: cvPoint).

    Hang on, you are using char text[80]!

    I think that is again a source of problems. It is best to switch it to

    #include <sstream>
    //...
    stringstream ss;
    ss << "Rect(" << pt1.x << ", " << ... << ")";
    //...
    putText(img_show, ss.str(), Point(10,20), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 255) );
    

    Note that the font (I use) is not 10, but FONT_HERSHEY_PLAIN (or some other FONT_ constant).

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

Sidebar

Related Questions

How can I subtract one image from another using openCV? Ps.: I coudn't use
I use tesseract and opencv in my project. But the problem is when I
I'm trying to recognize rectangular boxes using Kinect, I know I can use OpenCV
Can I make some c++ webcam program without using opencv (and without others external
Can anyone describe how can i implement SWT in python using opencv or simplecv
Are there any opencv function like cvHoughCircles() that can use for square detection programming
I am using OpenCV library. It doesn't have a modulo operator that can be
The image resizing function provided by Emgu (a .net wrapper for OpenCV) can use
Using OpenCV in Python, how can I create a new RGB image? I don't
I am trying to create a C++ wrapper for OpenCV, but unfortunately can't get

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.