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:
Read More:
- [Solved] Opencv3. X fatal error: opencv2/nonfree/nonfree.hpp: there is no such file or directory
- error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘cv::imshow‘
- [Solved] OpenCV Error: AttributeError: module ‘cv2‘ has no attribute ‘data‘
- [Solved] VScode Run C++ File Error: fatal error:opencv2\core.hpp:No such file or diretory
- Opencv Can not Find opencv2/gpu/gpu.hpp [How to Solve]
- [Solved] Unity Error: Assertion failed on expression: ‘IsMatrixValid(matrix)‘…
- [Solved] OpenCv5 error: (-215:Assertion failed) !_descriptors.empty() in function ‘add‘
- OpenCV4 Error: imread(argv[1], CV_LOAD_IMAGE_COLOR);
- [Solved] Opencv Calls PB Model Error: cv2.error: OpenCV(4.2.0)…
- [Solved] Opencv error: assertion__acrt_first_block == header
- [Solved] SyntaxError:JSON.parse:unexpected character at line 1 column 1 of the JSON data
- [Solved] Failed assertion: line 3180 pos 12: ‘debugNeedsPaint‘: is not true
- [Solved] flutter Project Error: ScrollController attached to multiple scroll views, Failed assertion: line 109 pos 12
- [Solved] import cv2 Error: ImportError: libGL.so.1: cannot open shared object file: No such file or directory
- [Solved] Cmake compile opencv open-source project Error: but it set OpenCV_FOUND to FALSE so package “OpenCV” is considered to be…
- [Solved] src/delly.h:8:42: fatal error: boost/graph/adjacency_list.hpp: No such file or directory
- Opencv c++ Read Video Error: capture.isOpened() Return false
- [TensorRT] INTERNAL ERROR: Assertion failed: mem = nullpt
- [Solved] OpenCV ERROR: The minSdk version should not be declared in the android manifest file
- [Solved] error: ‘CV_GRAY2BGR’ was not declared in this scope