-2

When I try to run the code below in Visual Studio I getting an error message:

Unhandled exception at 0x00007FFC976DA839 in TestOpenCV.exe: Microsoft C++ exception: cv::Exception at memory location 0x0000003DAEEFF360.

And the command window show this message:

OpenCV(4.1.0-dev) Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file C:\Users\hordon\opencv\modules\highgui\src\window.cpp, line 352

I can't figure out whats is wrong. My best guess is that I use a virtual Windows 10 on a Mac with Parallels Desktop and something is wrong with the path. But that would be strange since I copied the image directly next to the EXE. Any idea how to fix this?

#include "pch.h"
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

cv::Mat image;

void myMouseCallback(int event, int x, int y, int flags, void* userdata)
{
    if (flags & cv::MouseEventFlags::EVENT_FLAG_LBUTTON) {
        if (flags & cv::MouseEventFlags::EVENT_FLAG_SHIFTKEY) {
            cv::rectangle(image, { x,y }, { y, x }, CV_RGB(255, (x*y) % 255, (x + y) % 255));
        } else if (flags &cv::MouseEventFlags::EVENT_FLAG_ALTKEY) {
            cv::line(image, { x, y }, { y, x }, CV_RGB((y * x) % 255, (int)(sin((double)x / image.cols) * 255), (x * y * x - y) % 255));
        } else {
            cv::circle(image, { x,y }, (x + y) % 10, 0);
        }
    }

    cv::imshow("Ablak", image);
}

int main()
{
    image = cv::imread(".\\csillam.jpg");

    cv::Mat greyScale(image.rows, image.cols, CV_8UC1);
    for (int i = 0; i < greyScale.rows; ++i) {
        for (int j = 0; j < greyScale.cols; j++) {
            cv::Vec3b pixel = image.at<cv::Vec3b>(i, j);

            greyScale.at<uint8_t>(i, j) = (pixel[0] + pixel[1] + pixel[2]) / 3;
        }
    }
    cv::imshow("Greyscale", greyScale);

    cv::imshow("Ablak", image);
    cv::setMouseCallback("Ablak", myMouseCallback);

    cv::waitKey(0);
}
Hordon
  • 603
  • 2
  • 9
  • 21

2 Answers2

0

Check if the image was read correctly before further processing

image = cv::imread(".\\csillam.jpg");
if (!image.data)    // image was not created 
{
    exit(1);
}
seccpur
  • 4,996
  • 2
  • 13
  • 21
  • Thank you! The problem was with the image, not with the code. From now I will check this first always. – Hordon Jun 19 '19 at 15:25
0

But that would be strange since I copied the image directly next to the EXE

By default, Visual studio will set working directory elsewhere. You can find your current working directory or change it

zteffi
  • 660
  • 5
  • 18