Compiling OpenCV Based Script With G++ - c++

I want to begin writing saying that I know that this topic has been talked about multiple times. I can see several posts with similar titles as I'm typing this now. The reason that I'm asking this question once again is because I've tried what those posts stay, and it still won't work. I've copied a simple script that upscales an image using opencv in C++ to begin the learning journey and to make a script that I hope to use in the future very often, maybe even share on GitHub. With that being said, here's more detail about my issue.
In my IDE, Visual Studio Code, my include path has been set to "/opt/homebrew/Cellar/opencv/4.6.0/include/opencv4/".
I've tried using the -I arguments, -l arguments, whatever this is: "pkg-config --cflags --libs opencv4", but none of that worked.
The only time that I could get it to work is using the following command, but even then, g++ returned: fatal error: too many errors emitted, stopping now [-ferror-limit=] 41 warnings and 20 errors generated.
Here's the program that I have:
#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main() {
Mat frame = imread("/Users/karsoneskind/Desktop/Programming/C++/ibootimformer/tests/image.jpg");
namedWindow("Output", 0);
namedWindow("Input", 0);
Mat output;
resize(frame, output, Size(1000, 200), 0, 0);
imshow("Output", output);
imshow("Input", frame);
waitKey(0);
}
Edit: This is the command in terminal I used to try to compile the code:
g++ test.cpp -o test pkg-config --cflags --libs opencv4

Related

Can't cross-compile opencv linux-arm on c++ for RPI

(This is the first time I ask question in stackoverflow. And I'm not fluent in English. sorry for not asking clearly)
I am trying to cross-compile opencv based c++ code on Ubuntu(using virtual box) and try to use it on RPI 4.
I use Ubuntu 16.04(using virtual box) and downloaded OpenCV 4.0.1 refer to this site https://webnautes.tistory.com/1030?category=704653
And I down loaded cross-compiiler by sudo apt-get install git gcc-arm-linux-gnueabihf make ncurses-dev build-essential
After that I wrote c++ code like this.
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int ac, char** av){
Mat img =imread("/home/laboman/ex/ex.jpeg", 0);
Mat img_edge;
Canny(img, img_edge, 50, 200);
imshow("original", img);
imshow("img_edge", img_edge);
waitkey(0);
return 0;
}
It works well when I just compile it on Ubuntu.
But when I tried to cross-compile it
arm-linux-gnueabihf-gcc -o qwer /home/laboman/ex/ex.cpp $(pkg-config opencv4 --libs --cflags) -std=c++11
it came out with an error
/usr/local/lib/libopencv_gapi.so: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
First, I thought there are no libopencv_gapi.so on /usr/local/lib but there were on that file.
I think the problem is that arm-linux cannot cognize(I'm not sure this is right word) the opencv when cross-compiling.
Don't know how cross-compiling can cognize the opencv.
Please tell me what I have to do?
P.S. I also tried old version of OpenCV OpenCV 2.4.9.1

OpenCV output monochrome TIFF group 4 compression

Is there a way to output monochrome TIFF files in OpenCV with group 4 compression?
This is the command to do it with imagemagick/graphicsmagick
'gm convert '.$file.' -type bilevel -monochrome -compress group4 '.$output
OpenCV version 4.5.1
update
# g++ -Wall -O3 -std=c++17 main.cpp -o main `pkg-config opencv4 --cflags --libs` -ltiff
find doesn't return anything
root#x:/usr/include/opencv4/opencv2# find . -name "*tif*"
code
#include <tiffio.h>
#include <opencv4/opencv2/opencv.hpp>
std::vector<int> params = {cv::IMWRITE_TIFF_COMPRESSION, COMPRESSION_CCITTFAX4};
cv::imwrite(_output, src_bin, params);
error
[ WARN:0] global ../modules/imgcodecs/src/grfmt_tiff.cpp (914)
writeLibTiff OpenCV TIFF(line 914): failed TIFFSetField(tif,
TIFFTAG_PREDICTOR, predictor) imwrite_('out.tif'): can't write data:
OpenCV(4.5.1) ../modules/imgcodecs/src/grfmt_tiff.cpp:914: error:
(-2:Unspecified error) OpenCV TIFF: failed TIFFSetField(tif,
TIFFTAG_PREDICTOR, predictor) in function 'writeLibTiff'
OpenCV uses libtiff (link). If you call cv.imwrite (link) you can pass additional parameters.
For group 4 compression the docs say that you have to pass the IMWRITE_TIFF_COMPRESSION flag (link) with value COMPRESSION_CCITTFAX4 (link).
Example:
#include <tiffio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
vector<int> params = {IMWRITE_TIFF_COMPRESSION, COMPRESSION_CCITTFAX4};
bool success = imwrite("image.tif", img, params);
Update
Well, this is how it should work. But it seems that opencv4 currently has one or more issues in writing Group 4 TIFF. Btw it's the same when you try the python-lib. This known issue deals with a predictor tag that is wrongly added by opencv. I applied the fix to my local build but after that one more issue came up missing a conversion of the 8bit Mat to 1bit/sample for group4. So my recommendation is:
Workaround
Instead you can use the GraphicsMagick API Magick++.
#include <opencv2/opencv.hpp>
#include <Magick++.h>
using namespace cv;
using namespace Magick;
int main(int argc, char* argv[]) {
InitializeMagick("");
Mat img = imread("img.png");
Image image(img.cols, img.rows, "BGR", CharPixel, (char *)img.data);
image.compressType(CompressionType::Group4Compression);
image.write("img.tif");
return(0);
}

Undefined reference to cv::Mat::Mat()

I made a simple c++ code that reads the webcam image and display it. However, when I compile, I get the error - 'Undefined reference to cv::Mat::Mat()'. I don't know why it shows two Mat's. Here is my code:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
#include <stdlib.h>
int main() {
cv::VideoCapture cap(0);
if (!cap.isOpened){
std::cout << "Error opening camera" << std::endl;
}
cv::Mat img;
while(1){
cap >> img;
cv::imshow("output", img);
cv::waitKey(1);
}
}
This is how I compile it
g++ example.cpp `pkg-config --libs opencv4`
I can't figure out why the error shows up. Any help is appreciated!
This works on my Linux:
g++ main.cpp -I/usr/include/opencv4/ -lopencv_core -lopencv_videoio -lopencv_highgui
While this is not portable, (using cmake would do the trick, but you'd need to learn cmake first), I'll give you a hint how you can discover this yourself.
Whenever you see an error like Undefined reference to cv::Mat::Mat(), go to the documentation at https://docs.opencv.org/ , chose the newest version (here: https://docs.opencv.org/4.5.5/ ), enter, in the "search" window, the name of a class/function the linker cannot find (here: Mat), read the header that defines it (here: #include<opencv2/core/mat.hpp>), then the missing library will have the name libopencv_core.* or libopencv_mat.*. Find whichever is in your machine (e.g. inside /user/lib) and link it, omitting the extension and the beginning lib in the name. In my case the library location, with the full path, is /usr/lib/libopencv_core.so, so I link it with -lopencv_core. Then you need to find the remaining libs in the same way.
Learn how to automatize this, e.g. via a Makefile, CMakeLists.txt or just a simple bash script.

OpenCV: Undefined reference to xcb_poll_for_reply

As of late I have been getting the following error whenever I try to compile any program that uses the open cv libraries, I use g++ to compile:
g++ Example.cpp -o Ex `pkg-config opencv --cflags --libs`
No matter the content of the file (I have checked with programs that worked a couple of weeks ago) I always get the following error:
/usr/lib64/libX11.so.6: undefined reference to `xcb_poll_for_reply64'
/usr/lib64/libX11.so.6: undefined reference to `xcb_wait_for_reply64'
Do you have any idea of what might be the cause? (and how to fix it)
An example program that fails to compile:
#include "path/opencv2/highgui/highgui.hpp"
#include "path/opencv/highgui.h"
using namespace cv;
int main (int argc, char * argv[])
{
Mat image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE) ;
return 0;
}
Add -lxcb to your command line (this will instruct the linker linking w/ the xcb library). Please make sure the 64b version of xcb is in the linker path (you can always put it explicitly via the -L switch)
The error was caused by some changes done to the libX11.so.6, talked with the FE machines support and they fixed it.

Using cvLoad I get Unspecified error (The node does not represent a user object (unknown type?)) error

here is the full error message:
OpenCV Error: Unspecified error (The node does not represent
a user object (unknown type?)) in cvRead, file
/home/abe/Documents/opencv-2.4.6.1/modules/core/src/persistence.cpp, line 4997
I saw on google that this might be a bug here
http://opencv-users.1802565.n2.nabble.com/face-detection-cvLoad-td4467872.html
here is how im running it
#include <cv.h>
#include <highgui.h>
using namespace std;
int main()
{
cvLoad("/home/abe/Documents/opencv-2.4.6.1/data/haarcascades/haarcascade_frontalface_alt2.xml");
return 0;
}
many programs load xml files with this function and this file is from opencv directory so i know its good I tried the workaround using cvErode in the above link but still got same error any help is appreciated
btw compile on ubuntu raring with this which works for every other program
g++ b.cpp -o b `pkg-config --cflags --libs opencv`