view cv::Mat data while debugging in Visual Studio Code - c++

I need to check elements of some cv::Mats while debugging my OpenCV C++ code.
Tried adding mymat.at<double>(0, 0) to watch but there are two problems:
Sometimes it doesn't work: Couldn't find method cv::Mat::at<double>
Even if it works, it is very hard to check elements one by one.
Currently, I write Mat contents to a file using cv::FileStorage but it is not a good solution also.
Is there any VSCode extension like Image Watch to show value of Mat elements while debugging?

Related

OpenCV destroyWindow() not working with multiple windows

I am using openCV library to open and display multiple images. I am doing this with multiple windows created in order to display each image. In order to achieve display of multiple windows at the same time, I am using waitKey() only after the last image.
cv::namedWindow("Window1");
cv::imshow("Window1", myImage1);
cv::namedWindow("Window2");
cv::imshow("Window2", myImage2);
cv::waitKey(1000);
As can be seen from the code, my goal is to give the user 1s of time to press any key, otherwise I want to destroy one of the windows (for the purpose of this question it can be either one). I want to achieve this by using openCV's function destroyWindow().
Below my entire code can be seen:
cv::namedWindow("Window1");
cv::imshow("Window1", myImage1);
cv::namedWindow("Window2");
cv::imshow("Window2", myImage2);
cv::waitKey(1000);
cv::destroyWindow("Window2");
The goal of this code snippet should be that only "Window1" remains displayed, if 1s goes by, with the user not pressing any key.
However, this does not happen. The end result is that none of the windows are destroyed.
I have tested the following code snippet, which results in both windows being closed:
cv::namedWindow("Window1");
cv::imshow("Window1", myImage1);
cv::namedWindow("Window2");
cv::imshow("Window2", myImage2);
cv::waitKey(1000);
cv::destroyWindow("Window1");
cv::destroyWindow("Window2");
The same results when I use destroyAllWindows() function (which makes sense).
My question now is, why can't I destroy only one of the windows?
Additional info:
Using Ubuntu 20.04.
OpenCV version is 4.2.
Working in C++
Changing the order of which window I want to destroy changes nothing.
Tried to replicate it, facing this issue in Python as well on Ubuntu. If you are still stuck, you can try a stopgap solution of reshowing only the one you wanted to show provided the user has pressed a key or not by storing the result of waitKey in some variable. If it is -1 then no key has been pressed.
I have provided a sample solution in Python which you shouldn't face any difficulties converting to C++.
import cv2
img1 = cv2.imread('img1.png')
img2 = cv2.imread('img2.png')
cv2.namedWindow('img1')
cv2.imshow('img1', img1)
cv2.namedWindow('img2')
cv2.imshow('img2', img2)
key = cv2.waitKey(5000)
if key == -1:
cv2.destroyAllWindows()
cv2.imshow('img1', img1)
cv2.waitKey(0)
else:
# do whatever destroy both or keep on showing both using cv2.waitKey(0)
cv2.destroyAllWindows()
I have reached a solution by adding startWindowThread() before adding each of the windows.
An important thing to note is also that I have built openCV using GTK option, so my solution is tested only on GTK not on others.
startWindowThread() is used only with GTK as noted here: https://github.com/opencv/opencv/issues/7562 - for others the function is empty.

Qt program using Matlab dll, Initialized successfully in local disk, but failed in My U disk

I am writing a Qt program using matlab dll, the matlab function is very simple, However I encountered some exception caused by initializing problem. So I have changed my part of code as below.
void DicomViewer::on_pushButton_clicked()
{
if(libMyAddInitialize()){
mwArray dicomArray2=Mat2mwArray(dicomMat1);
//c_matlab(1,dicomArray2,Mat2mwArray(dicomMat1));
mwArry2Mat(dicomArray2).copyTo(dicomMat2);
dicomImgShow(TEMPDICOM2);
libMyAddTerminate();
}
}
This part of the code provides a button to change my cv::Mat data. I am using two functions Mat2mwArrayandmwArray2Mat to convert Mat between mwArray. Funciton dicomImgShow is to show the Mat to My program. That I think code is not the key problem.
My Key problem is The button function only work in my local disk (C:/ and D:/). I put it into my U disk and it failed. I also tried to run it in admin mode, but failed again. So I wonder what's wrong in my program.
Thanks for anyone who come to help .

Opencv Mosaic from video

I need to do the same thing of this video https://youtu.be/59RJeLlDAxQ but in Opencv. For now I'm doing this thing http://ramsrigoutham.com/2012/11/22/panorama-image-stitching-in-opencv/ with a little modify in the final image merging, but it doesn't work very well. How can I proceed?
EDIT
For testing I'm using the video lab from this page http://www.cs.ucsb.edu/~holl/CS290I/Assignments/Assignments-3/Assignment3Mosaicing.html
I ran my code on that video and I obtain this:
It's not very accurate but its ok.If I let the program run, at a certain point my stitcher produce this:
.
For the stitching ROIs instead of the ramsrigoutham.com ones I'm using this:
warpPerspective(current_frame, rImg, H, Size(current_frame.cols, current_frame.rows), INTER_NEAREST);
Mat roi1(final_img, Rect(img_loop.cols, img_loop.rows, vImg[1].cols, vImg[1].rows));
Mat roi2(final_img, Rect(img_loop.cols, img_loop.rows, rImg.cols, rImg.rows));
rImg.copyTo(roi2);
vImg[1].copyTo(roi1);
Why not using: http://docs.opencv.org/modules/stitching/doc/high_level.html#stitcher-composepanorama
It's available on 2.4.11 and 3.0.0.
The link you mentioned is access deniedhttp://www.cs.ucsb.edu/~holl/CS290I/Assignments/Assignments-3/Assignment3Mosaicing.html
.
what are 'img_loop' and 'Vimg' and 'rimg' in your code? there is some difference between your code and the code you linked.if it is possible explain a little so I can work on your problem , cause Im doing the same thing in opencv

Morphology from Magick++ to match Command line Convert

I am trying to duplicate the ImageMagick command line:
Convert In.jpg ( -bias 50% -morphology Convolve LoG:0x1.5 ) Out.jpg
via the Magick++ in C++ (visual Studio 2010 Express)
I have read that the morphological operations are not part of Magic++ so the C MagickCore functions need to be used. I am unable to obtain output that matches (or is even close) to
ImageMagick command line output.
I am attempting to create the Kernel via
k=MagickCore::AcquireKernelInfo("LoG:0x1.5");
and execute the morphology via
m = MagickCore::MorphologyImage(i, MagickCore::ConvolveMorphology, 3, k, e);
I am guessing at these methods and parameters due to lack of information on specifics.
Does anyone have guidance on how to accomplish the same output from C++ ?
To accomplish these operations in Magick++ would require many tedious steps that I have no documentation on. The kernel creation is especially dubious as it came out as 13x13.
I found another way to accomplish the same goal: Use the MagickCore::ConvertImageComand(). The parameters are the same as the command line version and the output is the same. Using the command from C++ seems to work without problems...
using namespace Magick;
char *args[]={"convert", "In.jpg","(","-bias","50%","-morphology", "convolve", "LoG:0x1.5", ")","Out.jpg" };
int args_count = 10;
MagickCore::ExceptionInfo *exception = MagickCore::AcquireExceptionInfo();
MagickCore::ImageInfo *image_info = MagickCore::AcquireImageInfo();
(void) strcpy(image_info->filename,"In.jpg");
image = MagickCore::ReadImage(image_info, exception);
MagickBooleanType status =
ConvertImageCommand(image_info, args_count, args, NULL, exception);
I would prefer to have this operation result in a buffer or in-memory image rather than writing to the disk but I guess that is another question...
The next version of ImageMagick (6.8.8-7) will have support for morphology in the Magick++ API. Your command:
convert In.jpg -bias 50% -morphology Convolve LoG:0x1.5 Out.jpg
can be written like this:
Magick::Image img;
img.read("In.jpg");
img.artifact("convolve:bias", "50%");
img.morphology(ConvolveMorphology, LoGKernel, "0x1.5");
img.write("Out.jpg");

How do I guard against failure of cvLoad?

I am writing a program that uses OpenCV and involves intrinsic and distortion parameters. These parameters are loaded from .xml files saved on disc. I use the following commands in my opening declarations to load the files:
CvMat *intrinsic = (CvMat*)cvLoad("Intrinsics.xml");
CvMat *distortion = (CvMat*)cvLoad("Distortion.xml");
This works fine as long as the files are in the program's working directory. When they are not, the program crashes without any indication of the nature of the error. I have made the mistake of not having the xml files located correctly multiple times before, and I would like to make this easier to troubleshoot in the future.
I would like to create a guard against the files failing to load. Perhaps if they are not present my program could display an error message and exit gracefully. I saw the method suggested here, and it should work for me, but I was wondering if there was a cleaner way to do it without including another header.
For example, the OpenCV function cvQueryFrame returns 0 if it does not return a frame. I use the code
frame = cvQueryFrame(capture);
if(!frame)
{
printf("ERROR: Could not get frame from webcam.");
exit(-1);
}
to exit the program if cvQueryFrame fails to return a frame. I would like to do something similar with my matrix loading commands. Is this possible, and if so, how should I do it?
I checked the OpenCV documentation and could not find a description of cvLoad's behaviour when it cannot find the file specified so I am not sure how to proceed.
I am writing this project in C++ and running it on Windows 7.
It works. Go ahead and try it yourself:
CvMat *distortion = (CvMat*)cvLoad("Distortion.xml");
if (!distortion)
{
printf("!!! cvLoad failed");
exit(-1);
}