Ask Your Question

Accessing the elements of a Mat variable

asked 2017-10-10 03:54:25 -0500

Digital Design gravatar image

updated 2017-10-10 04:00:16 -0500

Hi, I´m gonna find the maximum and the minimum value of an image pixels: // ConsoleApplication1.cpp : Defines the entry point for the console application. //

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

int main()
    Mat img1;
    double  minVal, maxVal;
    int minLoc[2], maxLoc[2];
    img1 = imread("DCIM_20171025_1.JPG");
    namedWindow("Test Image", WINDOW_AUTOSIZE);
    imshow("Test Image", img1);
    minMaxIdx(img1, &minVal, &maxVal, minLoc, maxLoc);
    printf("MAX= %d, MIN= %d", maxVal, minVal);
    return 0;

It seems pretty easy, but it doesn´t work! The program is compiled successfully, but there´ll be a run time error: Unhandled exception at 0x00007FF9E3EB9E08 in ConsoleApplication1.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000C981CFF3C0. occurred Does anyone know how to solve this problem? Thank you

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2017-10-10 04:11:51 -0500

berak gravatar image

2 problems here:

  1. "The function does not work with multi-channel arrays" see docs, again ! so you need to imread() it as grayscale: img1 = imread("DCIM_20171025_1.JPG", IMREAD_GRAYSCALE);

  2. careful with printf ! for doubles, you need %f not %d, so: printf("MAX= %f, MIN= %f", maxVal, minVal);

edit flag offensive delete link more


Thanks a lot. I thought that the pixels are integer! You are absolutely right :)

Digital Design gravatar imageDigital Design ( 2017-10-10 05:55:44 -0500 )edit

Question Tools

1 follower


Asked: 2017-10-10 03:54:25 -0500

Seen: 141 times

Last updated: Oct 10 '17