Ask Your Question
1

how to use imshow in class member?

asked 2014-01-28 11:24:17 -0600

Around gravatar image

updated 2014-01-28 11:24:58 -0600

The following code is used in a class member. Image watch shows the correct picture, but imshow() only gives a gray picture. Am I making mistakes? Thanks a lot.

void plane::Display()
{
using namespace cv;
Mat M(nPixel, nPixel, CV_8UC1);
double MAX = 0;
for (int i = 0; i < nPixel; i++)
        {
            for (int j = 0; j < nPixel; j++)
            {
                if (abs((*data)(i, j))>MAX)
                    MAX = pow(abs((*data)(i, j)), 2);
            }
        }

        for (int i = 0; i < nPixel; i++)
        {
            for (int j = 0; j < nPixel; j++)
            {
                M.at<uchar>(i, j) = pow(abs((*data)(i, j)), 2) / MAX * 255;
            }
        }
        namedWindow("Intensity", WINDOW_AUTOSIZE);
        imshow("Intensity", M);
}
edit retag flag offensive close merge delete

Comments

3

imshow() does not update unless you call waitKey()

berak gravatar imageberak ( 2014-01-28 11:26:41 -0600 )edit

Thanks a lot.

Around gravatar imageAround ( 2014-01-28 11:33:23 -0600 )edit

Do you want color?

keghn gravatar imagekeghn ( 2014-01-29 13:49:29 -0600 )edit

1 answer

Sort by » oldest newest most voted
-1

answered 2014-01-29 18:57:24 -0600

keghn gravatar image

#include<opencv2/highgui/highgui.hpp>

#include <opencv/cv.h>

#include <opencv/highgui.h>

#include <cmath>

#include <iostream>

using namespace cv;

using namespace std;

int main()

{
          Mat M = imread("scenetext03.jpg",CV_LOAD_IMAGE_COLOR);
          double MAX_blue = 0.0;
          double MAX_green = 0.0;
          double MAX_red = 0.0;

cout << "M.rows " << M.rows << "\n"; cout << "M.cols " << M.cols << "\n";

          for (unsigned int i = 0; i < M.rows; i++)
            {


              for (int unsigned j = 0; j < M.cols; j++)
                {
                  Vec3b data = M.at<Vec3b>(i, j);
                  if (abs(data.val[0]) > MAX_blue) MAX_blue = pow(abs(data.val[0]), 2);
                  if (abs(data.val[1]) > MAX_green) MAX_green = pow(abs(data.val[1]), 2);
                  if (abs(data.val[2]) > MAX_red) MAX_red = pow(abs(data.val[2]), 2);
                 }
             }

          for (int i = 0; i < M.rows; i++)
                {
                  for (int j = 0; j < M.cols; j++)
                        {
                          M.at<Vec3b>(i,j)[0] = pow(abs(M.at<Vec3b>(i,j)[0]), 2) / MAX_blue * 255;
                          M.at<Vec3b>(i,j)[1] = pow(abs(M.at<Vec3b>(i,j)[1]), 2) / MAX_blue * 255;
                          M.at<Vec3b>(i,j)[2] = pow(abs(M.at<Vec3b>(i,j)[2]), 2) / MAX_green * 255;
                         }
                 }

          namedWindow("Intensity", WINDOW_AUTOSIZE);
          imshow("Intensity", M);

          waitKey();

          destroyAllWindows(); //destroy all open windows

  return 0;
 }
edit flag offensive delete link more

Comments

Please, there is no reason for posting this code snippet. The solution was given by berak, because he simply forgot adding a waitKey to call the internal repainting function. In the future stop adding useless code snippets as they will get removed rather quickly...

StevenPuttemans gravatar imageStevenPuttemans ( 2014-01-30 06:30:53 -0600 )edit

Question Tools

Stats

Asked: 2014-01-28 11:24:17 -0600

Seen: 1,112 times

Last updated: Jan 29 '14