Tag Archives: OpenCV error

[Solved] error while loading shared libraries: libopencv_highgui.so.3.4: cannot open shared object file…

opencv problem:

./test1: error while loading shared libraries: libopencv_highgui.so.3.4: cannot open shared object file: No such file or directory

This is the problem I have when executing test1this file

error while loading shared libraries: indicates that there is a problem with the shared library.

Two methods are described below.

the first method

Check the dynamic library (shared library) configuration file

sudo gedit /etc/ld.so.conf

Make sure you have the first line of the path
insert image description here
and then update it

sudo ldconfig

The second method

Let’s first look at the shared library of this file that is executed

ldd test1

insert image description here
We can see that there are several libraries that cannot be found

Then we use the locatecommand to locate these libraries

Install the locate command

sudo apt install mlocate

Locate the missing libraries in turn

locate libopencv_highgui.so.3.4

insert image description here
The path is found, then we add the path

Go to the dynamic library configuration folder

cd /etc/ld.so.conf.d

Create a new .conf file, name it whatever you want

You can also add it to the original file without creating a new one.

I create a new opencv.conf file here

sudo vim opencv.conf

It seems that there is no need to add so many paths here

insert image description here

Once the file is saved, it’s time to update the shared library link

sudo ldconfig

After the update, you can delete the newly created .conf file without affecting it.

[Solved] Opencv Error: Error: Assertion failed (data) in cv::Mat::at, file … mat.inl.hpp, line 897(Accessed pixels of non-existent matrix)

Opencv4 error

This is the source code:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;

int main(int argc, const char* argv[]) {

	Mat src, dst;
	src = imread("./test.jpg");
	//if (src.empty()) {
	if (!src.data){
		printf("could not load image...\n");
		return -1;
	}
	//namedWindow("input img");
	//imshow("input img", src);

	//Three for loops, performing operations g_dstImage(i,j) =a*g_srcImage(i,j) + b
	for (int y = 0; y < src.rows; y++)
	{
		for (int x = 0; x < src.cols; x++)
		{
			for (int c = 0; c < 3; c++)
			{
				//g_dstImage.at<Vec3b>(y, x)[c] = saturate_cast<uchar>((g_nContrastValue*0.01)*(g_srcImage.at<Vec3b>(y, x)[c]) + g_nBrightValue);
				dst.at<Vec3b>(y, x)[c] = src.at<Vec3b>(y, x)[c];
			}
		}
	}


	namedWindow("output img");
	imshow("output img", dst);

	waitKey(0);
	return 0;
}

Vs compile run error:

reason

The nonexistent subscript of the access array (because we did not initialize the size and data type of mat array DST in the above code…)

Solution:

Add code: dst = Mat::zeros(src.size(), src.type());

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;

int main(int argc, const char* argv[]) {

	Mat src, dst;
	src = imread("./test.jpg");
	//if (src.empty()) {
	if (!src.data){
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("input img");
	imshow("input img", src);

	dst = Mat::zeros(src.size(), src.type());	

	//Three for loops, performing operations g_dstImage(i,j) =a*g_srcImage(i,j) + b
	for (int y = 0; y < src.rows; y++)
	{
		for (int x = 0; x < src.cols; x++)
		{
			for (int c = 0; c < 3; c++)
			{
				//g_dstImage.at<Vec3b>(y, x)[c] = saturate_cast<uchar>((g_nContrastValue*0.01)*(g_srcImage.at<Vec3b>(y, x)[c]) + g_nBrightValue);
				dst.at<Vec3b>(y, x)[c] = 255 - src.at<Vec3b>(y, x)[c];
			}
		}
	}


	namedWindow("output img");
	imshow("output img", dst);

	waitKey(0);
	return 0;
}

Vs compilation run result:

How to Solve Opencv Error: CPP: 1557 error

Opencv Error:
ret2, th2 = cv.threshold(img, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-c2l3r8zm\opencv\modules\imgproc\src\thresh.cpp:1557: error: (-2:Unspecified error) in function ‘double __cdecl cv::threshold(const class cv::_InputArray &,const class cv::_OutputArray &,double,double,int)’

THRESH_OTSU mode:
‘src_type == CV_8UC1 || src_type == CV_16UC1’
where
‘src_type’ is 16 (CV_8UC3)

Original Codes:

img = cv.imread('noisy.jpg')  
  
# Fixed threshold method
ret1, th1 = cv.threshold(img, 105, 255, cv.THRESH_BINARY)
# Otsu threshold method
ret2, th2 = cv.threshold(img, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
# Perform Gaussian filtering first, and then use Otsu threshold method  
blur = cv.GaussianBlur(img, (5, 5), 0)  
ret3, th3 = cv.threshold(blur, 105, 255, cv.THRESH_BINARY)  
ret4, th4 = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU) 

Problem analysis

The CV2.Threshold() function inputs graphics in the form of single channel, while the image in the above code is input in the form of color three channels, resulting in an error.

Solution:

Convert the color image into a single channel gray image for input.

img = cv.imread('noisy.jpg',0)

Rerun the program

Run successfully

[Solved] OpenCV error: #error “This header with legacy C API declarations has been removed from OpenCV.

Error reporting details

Error reporting reason

In the opencv version you are currently using, the definition declaration related to legacy C API has been removed, and the content of legacy can still be from legacy/constants_c.H from the header file.

Therefore, the solution is also obvious. Open the error file directly, delete the original part of the error header file, and replace it with legacy/constants_c.H.

Solution:

Open the cpp file with error
find the error header file location:

Delete the code of the header file containing the error and replace it with:

#include "opencv2/imgcodecs/legacy/constants_c.h"

Compiled successfully ~

if your problem is also solved, leave a praise~