Saturday, November 19, 2016

How to binarize image.

* Binary image has following steps:
** To convert image to gray scale.
** To check every pixel if greater than threshold, set value to maximum(255), otherwise minimum(0).

#include <cv.hpp>

using namespace cv;
#define THRESHOLD 100

int main( int argc, char** argv )
{
  Mat image;
  image = imread( "resource/lena.png", IMREAD_COLOR );
  Mat Output( image.rows, image.cols, CV_8UC3, Scalar(0, 0, 0) );
  int height = image.rows;
  int width = image.cols;

  for(int row = 0; row < height; row++) {
      for(int col = 0; col < width; col++) {
          uchar val = (image.at<Vec3b>(row, col)[0] * 0.2999 + image.at<Vec3b>(row, col)[1] * 0.587 + image.at<Vec3b>(row, col)[2] * 0.114);

          Output.at<Vec3b>(row, col)[0] = (val > THRESHOLD ? 255 : 0);
          Output.at<Vec3b>(row, col)[1] = (val > THRESHOLD ? 255 : 0);
          Output.at<Vec3b>(row, col)[2] = (val > THRESHOLD ? 255 : 0);
      }
  }

  namedWindow("out", WINDOW_NORMAL);
  imshow("out", Output);

  waitKey(0);

  return 0;
}
Result:

Friday, November 18, 2016

How to convert grayscale image.

#include <cv.hpp>

using namespace cv;

int main( int argc, char** argv )
{
  Mat image;
  image = imread( "resource/lena.png", IMREAD_COLOR );
  Mat Output( image.rows, image.cols, CV_8UC3, Scalar(0, 0, 0) );
  int height = image.rows;
  int width = image.cols;

  for(int row = 0; row < height; row++) {
      for(int col = 0; col < width; col++) {
          uchar val = (image.at<Vec3b>(row, col)[0] * 0.2999 + image.at<Vec3b>(row, col)[1] * 0.587 + image.at<Vec3b>(row, col)[2] * 0.114);
          Output.at<Vec3b>(row, col)[0] = val;
          Output.at<Vec3b>(row, col)[1] = val;
          Output.at<Vec3b>(row, col)[2] = val;
      }
  }

  namedWindow("out", WINDOW_NORMAL);
  imshow("out", Output);

  waitKey(0);

  return 0;
}

Result:


Thursday, November 17, 2016

To get RGB channel and output.

#include <cv.hpp>

using namespace cv;

int main( int argc, char** argv )
{
  Mat image;
  image = imread( "resource/lena.png", IMREAD_COLOR );
  Mat OutputR( image.rows, image.cols, CV_8UC3, Scalar(0, 0, 0) );
  Mat OutputG( image.rows, image.cols, CV_8UC3, Scalar(0, 0, 0) );
  Mat OutputB( image.rows, image.cols, CV_8UC3, Scalar(0, 0, 0) );
  int height = image.rows;
  int width = image.cols;

  for(int row = 0; row < height; row++) {
   for(int col = 0; col < width; col++) {

    OutputB.at<Vec3b>(row, col)[0] = image.at<Vec3b>(row, col)[0];
    OutputB.at<Vec3b>(row, col)[1] = 0;
    OutputB.at<Vec3b>(row, col)[2] = 0;
    OutputG.at<Vec3b>(row, col)[0] = 0;
    OutputG.at<Vec3b>(row, col)[1] = image.at<Vec3b>(row, col)[1];
    OutputG.at<Vec3b>(row, col)[2] = 0;
    OutputR.at<Vec3b>(row, col)[0] = 0;
    OutputR.at<Vec3b>(row, col)[1] = 0;
    OutputR.at<Vec3b>(row, col)[2] = image.at<Vec3b>(row, col)[2];
   }
  }

  namedWindow("outr", WINDOW_NORMAL);
  imshow("outr", OutputR);

  namedWindow("outg", WINDOW_NORMAL);
  imshow("outg", OutputG);

  namedWindow("outb", WINDOW_NORMAL);
  imshow("outb", OutputB);

  waitKey(0);

  return 0;
}


Results:

Using opencv to inverse Image.


#include <cv.hpp>

using namespace cv;

int main( int argc, char** argv )
{
  Mat image;
  int height, width;
  // load lena picture and convert to grayscale
  image = imread("resource/lena.png", IMREAD_GRAYSCALE);
  // store height and width
  height = image.rows;
  width = image.cols;
  // show image
  imshow("lena1", image);
  // inverse image
  for(int i = 0; i < height; i++) {
      for(int j = 0; j < width; j++) {
          image.at<uchar>(i, j) = 255 - image.at<uchar>(i,j);
      }
  }
  // show image again to compare
  imshow("lena2", image);

  waitKey(0);

  return 0;
}
Result:

Eclipse mars.2 opencv configuration for mac

How to configure opencv on eclipse mars.2 Release(4.5.2).
You have to install opencv3 by issue command "brew install opencv3"
* Elipse->Projects(right click)->Properties->C/C++ General->Paths and Symbols->Includes->GNU C++ -> Add
** /usr/local/Cellar/opencv3/3.1.0_2/include/opencv
** /usr/local/Cellar/opencv3/3.1.0_2/include

*  Elipse->Projects(right click)->Properties->C/C++ General->Paths and Symbols->Libraries -> Add
** opencv_core (required)
** opencv_imgproc (required)
** opencv_highgui (required)
** opencv_imgcodecs (required)
** opencv_flann
** opencv_video
** opencv_features2d
** opencv_calib3d
** opencv_ml

 *  Elipse->Projects(right click)->Properties->C/C++ General->Paths and Symbols->Library Paths->Add
** /usr/local/Cellar/opencv3/3.1.0_2/lib