Run tensorflow model to achieve super resolution with opencv dnn.hpp - opencv

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/imgproc.hpp"
#include<opencv2/opencv.hpp>
#include<opencv2/dnn.hpp>
#include <iostream>
using namespace std;
using namespace cv;
const size_t inWidth = 300;
const size_t inHeight = 300;
const float WHRatio = inWidth / (float)inHeight;
const char* classNames\[\] = { "background","face" };
void saveMat(cv::Mat inputMat,char* filename);
int main() {
String model = "opt_srcnn_vgg_test.pb";
dnn::Net net = cv::dnn::readNetFromTensorflow(model);
Mat img = imread("test.bmp");
Mat input;
img.copyTo(input);
cvtColor(input,input,CV_BGR2GRAY);//turn the rgb to gray
cout<<"load finish!"<<endl;
Mat inputBlob = dnn::blobFromImage(input, 1./255, Size(img.cols, img.rows));
imshow("input",input);
waitKey(0);
cout<<inputBlob.size;
net.setInput(inputBlob, "input");//set the network input,
cout<<"feed finish!"<<endl;
Mat pred = net.forward();
resize(pred,pred,Size(img.cols,img.rows));
imshow("result",pred);
waitKey(0);
return 0;
}
We tried to make opencv dnn use tensorflow trained .pb file to achieve super resolution. Our expected result is Figure 1, but we get the result of Figure 2 (Note: We determine that the .pb file is error-free). Why is this? Thank you!

Related

how to track objects within the ROI using dlib?

I'm trying to get the stream and defining the ROI and tracking an object within ROI using dlib correlation tracker but there is a error in compilation, not found any code or explanation which can describe any solution of this error. Thanks in advance.
cvcap.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "dlib/image_processing.h"
#include "dlib/gui_widgets.h"
#include "dlib/image_io.h"
#include "dlib/dir_nav.h"
using namespace dlib;
using namespace std;
int main(int argc, char * argv[]){
CvCapture* camera = cvCaptureFromFile(argv[1]);
if (camera==NULL)
printf("camera is null\n");
else
printf("camera is not null\n");
cvNamedWindow("CvCapture");
while (cvWaitKey(1000)!=atoi("q")){
double t1 = (double)cvGetTickCount();
IplImage *img = cvQueryFrame(camera);
cvSetImageROI(img, cvRect(140, 150, 340, 150));
IplImage *img1 = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
cvCopy(img, img1, NULL);
cvResetImageROI(img);
cvShowImage("ROI",img1);
correlation_tracker tracker;
tracker.start_track(img1, centered_rect(point(120,100), 80, 150));
image_window win;
tracker.update(img1);
win.set_image(img1);
win.clear_overlay();
win.add_overlay(tracker.get_position());
cout << "hit enter to process next frame" << endl;
cin.get();
double t2=(double)cvGetTickCount();
printf("time: %gms fps: %.2g\n",(t2-t1)/(cvGetTickFrequency()*1000.), 1000./((t2-t1)/(cvGetTickFrequency()*1000.)));
cvShowImage("CvCapture",img1);
}
cvReleaseCapture(&camera);
}
the error is :
In file included from dlib/image_processing.h:24:0,
from cvcap.cpp:3:
dlib/image_processing/correlation_tracker.h: In instantiation of ‘dlib::point_transform_affine dlib::correlation_tracker::make_chip(const image_type&,
dlib::drectangle, std::vector<dlib::matrix<std::complex<double> > >&) const [with image_type = _IplImage*]’:
dlib/image_processing/correlation_tracker.h:65:47: required from ‘void dlib::correlation_tracker::start_track(const image_type&, const dlib::drectang
le&) [with image_type = _IplImage*]’
cvcap.cpp:36:70: required from here
dlib/image_processing/correlation_tracker.h:301:67: error: invalid use of incomplete type ‘struct dlib::image_traits<_IplImage*>’
typedef typename image_traits<image_type>::pixel_type pixel_type;
Update
i edited my code and now it is compiling but when i'm running my code, tracking box appears but it is not tracking the objects.
for(;;) {
if(!vcap.read(image)) {
std::cout << "No frame" << std::endl;
break;
} else {
std::cout << "Starting" << std::endl;
array2d<rgb_pixel> img;
assign_image(img, cv_image<rgb_pixel>(image));
tracker.start_track(img, centered_rect(point(413,260), 98, 126));
for (unsigned long i = 0; i < img.size(); i++) {
tracker.update(img);
win.set_image(img);
win.clear_overlay();
win.add_overlay(tracker.get_position());
}
}
}
You're passing an IplImage to the dlib routine. But if you look at the docs it says you have to convert your opencv images to cv_images before passing them to dlib functions. And you can see this with the error pointing out IplImage doesn't have a pixel_type trait. A fix would be.
IplImage *img1 = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
...
correlation_tracker tracker;
// Not sure if the tracker wants a single channel image
// change rgb_alpha_pixel to uchar in that case.
tracker.start_track(dlib::cv_image<dlib::rgb_alpha_pixel>(img1), centered_rect(point(120,100), 80, 150));
Pixel types and the Correlation tracker
EDIT: Passed your stuff to opencv 3 and this is compiling for me. Do you really need to use opencv 2?
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "dlib/image_processing.h"
#include "dlib/gui_widgets.h"
#include "dlib/image_io.h"
#include "dlib/dir_nav.h"
#include <dlib/opencv/cv_image.h>
using namespace cv;
using namespace dlib;
using namespace std;
int main(int argc, char * argv[]){
VideoCapture cap(0);
Mat frame;
while (cap.read(frame)) {
cvtColor(frame, frame, CV_RGB2GRAY);
cv_image<unsigned char> img(frame);
correlation_tracker tracker;
tracker.start_track(img, centered_rect(point(120,100), 80, 150));
image_window win;
tracker.update(img);
win.set_image(img);
win.clear_overlay();
win.add_overlay(tracker.get_position());
cout << "hit enter to process next frame" << endl;
cv::waitKey(0);
}
}
and with an uglier opencv 2..
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "dlib/image_processing.h"
#include "dlib/gui_widgets.h"
#include "dlib/image_io.h"
#include "dlib/dir_nav.h"
#include <dlib/opencv/cv_image.h>
using namespace cv;
using namespace dlib;
using namespace std;
int main(int argc, char * argv[]){
CvCapture* camera = cvCaptureFromFile(argv[1]);
if (camera==NULL)
printf("camera is null\n");
else
printf("camera is not null\n");
cvNamedWindow("CvCapture");
while (cvWaitKey(1000)!=atoi("q")) {
double t1 = (double)cvGetTickCount();
IplImage *img = cvQueryFrame(camera);
cvSetImageROI(img, cvRect(140, 150, 340, 150));
IplImage *img1 = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
cvCopy(img, img1, NULL);
cvResetImageROI(img);
cvShowImage("ROI",img1);
cvCvtColor(img1, img1, CV_RGB2GRAY);
cv_image<unsigned char> img2(img1);
correlation_tracker tracker;
tracker.start_track(img2, centered_rect(point(120,100), 80, 150));
image_window win;
tracker.update(img2);
win.set_image(img2);
win.clear_overlay();
win.add_overlay(tracker.get_position());
cout << "hit enter to process next frame" << endl;
cin.get();
double t2=(double)cvGetTickCount();
printf("time: %gms fps: %.2g\n",(t2-t1)/(cvGetTickFrequency()*1000.), 1000./((t2-t1)/(cvGetTickFrequency()*1000.)));
cvShowImage("CvCapture",img1);
}
cvReleaseCapture(&camera);
}
As you see the error came exactly from what I told you before.

Why is OpenCV Gpu module performing faster than VisionWorks?

I have tried several functions of OpenCv gpu module and compared the same behavior with visionWorks immediate code. And surprisingly, it all circumstances the OpenCv Gpu Module is performing significantly faster than VisionWorks.
e-g
a Gaussian pyramid of level 4 implemented manually using opencv
#include <iostream>
#include <stdio.h>
#include <stdio.h>
#include <queue>
/* OPENCV RELATED */
#include <cv.h>
#include <highgui.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/stitching/detail/util.hpp"
#include "opencv2/stitching/detail/warpers.hpp"
#include "opencv2/stitching/warpers.hpp"
#include <opencv2/gpu/gpu.hpp>
#include "opencv2/opencv_modules.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/detail/autocalib.hpp"
#include "opencv2/stitching/detail/blenders.hpp"
#include "opencv2/stitching/detail/camera.hpp"
#include "opencv2/stitching/detail/exposure_compensate.hpp"
#include "opencv2/stitching/detail/matchers.hpp"
#include "opencv2/stitching/detail/motion_estimators.hpp"
#include "opencv2/stitching/detail/seam_finders.hpp"
#include "opencv2/stitching/detail/util.hpp"
#include "opencv2/stitching/detail/warpers.hpp"
#include "opencv2/stitching/warpers.hpp"
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
using namespace gpu;
using namespace cv::detail;
int main()
{
Mat m = imread("br1.png");
GpuMat d_m = GpuMat (m);
GpuMat d_m2;
GpuMat l1,l2,l3,l4;
int iter = 100;
int64 e = getTickCount();
float sum = 0;
sum = 0;
for(int i = 0 ; i < iter; i++)
{
e = getTickCount();
gpu::pyrDown(d_m,l1);
gpu::pyrDown(l1,l2);
gpu::pyrDown(l2,l3);
gpu::pyrDown(l3,l4);
sum+= (getTickCount() - e) / getTickFrequency();
}
cout <<"Time taken by Gussian Pyramid Level 4 \t\t\t"<<sum/iter<<" sec"<<endl;
//imwrite("cv_res.jpg",res);
return 0;
}
takes 2.5 ms on average for 100 iterations. Whereas, VisionWorks
#include <VX/vx.h>
#include <VX/vxu.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <stdio.h>
#include <queue>
/* OPENCV RELATED */
#include <cv.h>
#include <highgui.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/stitching/detail/util.hpp"
#include "opencv2/stitching/detail/warpers.hpp"
#include "opencv2/stitching/warpers.hpp"
#include <opencv2/gpu/gpu.hpp>
#include "opencv2/opencv_modules.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/detail/autocalib.hpp"
#include "opencv2/stitching/detail/blenders.hpp"
#include "opencv2/stitching/detail/camera.hpp"
#include "opencv2/stitching/detail/exposure_compensate.hpp"
#include "opencv2/stitching/detail/matchers.hpp"
#include "opencv2/stitching/detail/motion_estimators.hpp"
#include "opencv2/stitching/detail/seam_finders.hpp"
#include "opencv2/stitching/detail/util.hpp"
#include "opencv2/stitching/detail/warpers.hpp"
#include "opencv2/stitching/warpers.hpp"
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
using namespace gpu;
using namespace cv::detail;
vx_image createImageFromMat(vx_context& context, cv::Mat& mat);
vx_status createMatFromImage(vx_image& image, cv::Mat& mat);
/* Entry point. */
int main(int argc,char* argv[])
{
Mat cv_src1 = imread("br1.png", IMREAD_GRAYSCALE);
int width = 1280;
int height = 720;
int half_width = width/2;
int half_height = height/2;
Mat dstMat(cv_src1.size(), cv_src1.type());
Mat half_dstMat(Size(width/16,height/16),cv_src1.type());
/* Image data. */
if (cv_src1.empty() )
{
std::cerr << "Can't load input images" << std::endl;
return -1;
}
/* Create our context. */
vx_context context = vxCreateContext();
/* Image to process. */
vx_image image = createImageFromMat(context, cv_src1);
//NVXIO_CHECK_REFERENCE(image);
/* Intermediate images. */
vx_image dx = vxCreateImage(context, width, height, VX_DF_IMAGE_S16);
vx_image dy = vxCreateImage(context, width, height, VX_DF_IMAGE_S16);
vx_image mag = vxCreateImage(context, width, height, VX_DF_IMAGE_S16);
vx_image half_image = vxCreateImage(context, half_width, half_height, VX_DF_IMAGE_U8);
vx_image half_image_2 = vxCreateImage(context, half_width/2, half_height/2, VX_DF_IMAGE_U8);
vx_image half_image_3 = vxCreateImage(context, half_width/4, half_height/4, VX_DF_IMAGE_U8);
vx_image half_image_4 = vxCreateImage(context, half_width/8, half_height/8, VX_DF_IMAGE_U8);
int64 e = getTickCount();
int iter = 100;
float sum = 0.0;
e = getTickCount();
iter = 100;
for(int i = 0 ; i < iter; i ++)
{
/* RESIZEZ OPERATION */
if(vxuHalfScaleGaussian(context,image,half_image,3) != VX_SUCCESS)
{
cout <<"ERROR :"<<"failed to perform scaling"<<endl;
}
if(vxuHalfScaleGaussian(context,half_image,half_image_2,3) != VX_SUCCESS)
{
cout <<"ERROR :"<<"failed to perform scaling"<<endl;
}
if(vxuHalfScaleGaussian(context,half_image_2,half_image_3,3) != VX_SUCCESS)
{
cout <<"ERROR :"<<"failed to perform scaling"<<endl;
}
if(vxuHalfScaleGaussian(context,half_image_3,half_image_4,3) != VX_SUCCESS)
{
cout <<"ERROR :"<<"failed to perform scaling"<<endl;
}
sum += (getTickCount() - e) / getTickFrequency();
}
cout <<"Resize to half " <<sum/iter<<endl;
createMatFromImage(half_image_4,half_dstMat);
imwrite("RES.jpg",half_dstMat);
/* Tidy up. */
vxReleaseImage(&dx);
vxReleaseImage(&dy);
vxReleaseImage(&mag);
vxReleaseContext(&context);
}
vx_image createImageFromMat(vx_context& context, cv::Mat& mat)
{
vx_imagepatch_addressing_t src_addr = {
mat.cols, mat.rows, sizeof(vx_uint8), mat.cols * sizeof(vx_uint8), VX_SCALE_UNITY, VX_SCALE_UNITY, 1, 1 };
void* src_ptr = mat.data;
vx_image image = vxCreateImageFromHandle(context, VX_DF_IMAGE_U8, &src_addr, &src_ptr, VX_IMPORT_TYPE_HOST);
return image;
}
vx_status createMatFromImage(vx_image& image, cv::Mat& mat)
{
vx_status status = VX_SUCCESS;
vx_uint8 *ptr = NULL;
cout <<"Creating image "<<mat.cols << " " <<mat.rows <<endl;
vx_rectangle_t rect;
vxGetValidRegionImage(image, &rect);
vx_imagepatch_addressing_t addr = {
mat.cols, mat.rows, sizeof(vx_uint8), mat.cols * sizeof(vx_uint8), VX_SCALE_UNITY, VX_SCALE_UNITY, 1, 1 };
status = vxAccessImagePatch(image, &rect, 0, &addr, (void **)&ptr, VX_READ_ONLY);
mat.data = ptr;
return status;
}
takes 11.1 ms on single execution, and 96ms on average for 100 iterations.
If this is generally true, then what does visionWorks offer ?
I am running "cuda-repo-l4t-r21.3-6-5-local_6.5-50" version of L4T on Jetson TK1
You've made a mistake in VisionWorks code. You start timer only once e = getTickCount(); right before the loop, but you need to start it on each iteration.
iter = 100;
for(int i = 0 ; i < iter; i ++)
{
// START TIMER
e = getTickCount();
/* RESIZEZ OPERATION */
if(vxuHalfScaleGaussian(context,image,half_image,3) != VX_SUCCESS)
{
cout <<"ERROR :"<<"failed to perform scaling"<<endl;
}
if(vxuHalfScaleGaussian(context,half_image,half_image_2,3) != VX_SUCCESS)
{
cout <<"ERROR :"<<"failed to perform scaling"<<endl;
}
if(vxuHalfScaleGaussian(context,half_image_2,half_image_3,3) != VX_SUCCESS)
{
cout <<"ERROR :"<<"failed to perform scaling"<<endl;
}
if(vxuHalfScaleGaussian(context,half_image_3,half_image_4,3) != VX_SUCCESS)
{
cout <<"ERROR :"<<"failed to perform scaling"<<endl;
}
// STOP TIMER
sum += (getTickCount() - e) / getTickFrequency();
}
I think that the following code is mistake.
Mat cv_src1 = imread("br1.png", IMREAD_GRAYSCALE);
int width = 1280;
int height = 720;
I think that you should be set as follows.
Mat cv_src1 = imread("br1.png", IMREAD_GRAYSCALE);
vx_uint32 width = cv_src1.cols;
vx_uint32 height = cv_src1.rows;
And, I made sample code to reproduce.
But, VisionWorks(about 0.3ms) faster than GpuMat(about 0.4ms) on my environment.
https://gist.github.com/atinfinity/9c8c067db739b190ba17f2bd8dbe75d6
https://gist.github.com/atinfinity/e8c2f2da6486be51881e3924c13a311c
My environment is as follows.
GPU: NVIDIA GeForce GTX 680
OS: Windows 10 Pro 64bit
Compiler: Visual Studio 2013 Update5
VisionWorks:NVIDIA VisionWorks v1.0.25
OpenCV: OpenCV 3.1

Image Processing Opencv

I am having doubt in opencv. I'm trying to implement SURF algorithm. When I trying to build the code but I'm getting the following error.
*****error LNK2019: unresolved external symbol _cvExtractSURF referenced in function _main
1>SAMPLE.obj : error LNK2019: unresolved external symbol _cvSURFParams referenced in function _main*****
I have gone through all the posts related to my topic in this forum, but couldn't figure out the problem with my code. Please help me in resolving my problem.
code :
#include <stdio.h>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\calib3d\calib3d.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\legacy\legacy.hpp>
#include <opencv2\legacy\compat.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv\opensurf\surf.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
CvMemStorage* storage = cvCreateMemStorage(0);
cvNamedWindow("Image", 1);
int key = 0;
static CvScalar red_color[] ={0,0,255};
IplImage* capture= cvLoadImage( "testface.jpg");
CvMat* prevgray = 0, *image = 0, *gray =0;
while( key != 'q' )
{
int firstFrame = gray == 0;
IplImage* frame =capture;
if(!frame)
break;
if(!gray)
{
image = cvCreateMat(frame->height, frame->width, CV_8UC1);
}
//Convert the RGB image obtained from camera into Grayscale
cvCvtColor(frame, image, CV_BGR2GRAY);
//Define sequence for storing surf keypoints and descriptors
CvSeq *imageKeypoints = 0, *imageDescriptors = 0;
int i;
//Extract SURF points by initializing parameters
CvSURFParams params = cvSURFParams(500,1);
cvExtractSURF( image, 0, &imageKeypoints, &imageDescriptors, storage, params );
printf("Image Descriptors: %d\n", imageDescriptors->total);
//draw the keypoints on the captured frame
for( i = 0; i < imageKeypoints->total; i++ )
{
CvSURFPoint* r = (CvSURFPoint*)cvGetSeqElem( imageKeypoints, i );
CvPoint center;
int radius;
center.x = cvRound(r->pt.x);
center.y = cvRound(r->pt.y);
radius = cvRound(r->size*1.2/9.*2);
cvCircle( frame, center, radius, red_color[0], 1, 8, 0 );
}
cvShowImage( "Image", frame );
cvWaitKey(0);
}
cvDestroyWindow("Image");
return 0
}
Thank you,
Sreelakshmi Priya

OpenCV: How to use cvSobel?

I'm trying to find the gradient direction from the edges using OpenCv 2.4.5, but I'm having problem with cvSobel() and below is the error message and my code. I read somewhere that it might be due to the conversion between floating point(??) but I have no idea on how to fix it. Any Help??
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2\opencv.hpp>
#include <opencv2\calib3d\calib3d.hpp>
#include <iostream>
#include <stdlib.h>
#include "stdio.h"
using namespace cv;
using namespace std;
int main()
{
Mat im = imread("test1.jpg");
if (im.empty()) {
cout << "Cannot load image!" << endl;
}
Mat *dx, *dy;
dx = new Mat( Mat::zeros(im.rows, im.cols, 1));
dy = new Mat( Mat::zeros(im.rows, im.cols, 1));
imshow("Image", im);
// Convert Image to gray scale
Mat im_gray;
cvtColor(im, im_gray, CV_RGB2GRAY);
imshow("Gray", im_gray);
//trying to find the direction, but gives errors here
cvSobel(&im_gray, dx, 1,0,3);
waitKey(0);
return 0;
}
You are mixing the C++ and C api. cv::Mat is from the C++ api and CvArr* is from the C api.
here you are using The C api cvSobel on C++ classes.
//trying to find the direction, but gives errors here
cvSobel(&im_gray, dx, 1,0,3);
What happens if you do
cv::Sobel( im_gray, dx, im_gray.depth(), 1, 0, 3);
EDIT
and declare
Mat dx;
Mat dy;
I think this might solve your problem, I'm actually quite surprised your code compiles.

Can OpenCV be developed using C++ and C together

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;
}
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).

Resources