Block process an image? [closed]

asked 2015-02-21 14:46:29 -0600

Apastrix gravatar image

updated 2015-02-23 06:32:57 -0600

I want to :

  1. Read an image.
  2. Convert it to HSV and take only the Hue part.
  3. Divide the Hue plane into 16 blocks.
  4. Calculate for each block the Histogram and extract a feature based on it (maybe the dominant colour).
  5. Build a feature Vector.
  6. Compare the vector with itself.

Untill now i'm stuck at dividing the image into 16 blocks. The divide section is actually diving my image into 25 blocks rather than 16. So how can i divide my image into 16 blocks and process each one of them?

Code:

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

using namespace cv;
using namespace std;

int main()
{
  cv::Mat src;

  /// Load image
  src = cv::imread( "path", 1 );

  if( !src.data )
  {
      std::cout << "image not found" << std::endl;
      return -1;
  }

  /// convert the image to HSV space
  cv::Mat hsv;
  cv::cvtColor( src, hsv, COLOR_BGR2HSV );

  /// Separate the image in 3 places (H,S,V)
  cv::vector<Mat> hsv_channels;
  cv::split( src, hsv_channels );

  /// Take only the hue channel
  cv::Mat hue = hsv_channels[0];

  /// divide into 16 blocks
  int tileHeight = hue.rows/4;
  int tileWidth = hue.cols/4;

  for (int r = 0; r < hue.rows; r += tileHeight)
  {
      for (int c = 0; c < hue.cols; c += tileWidth)
     {
          cv::Mat tile = hue(cv::Range(r, min(r + tileHeight, hue.rows)),
                            cv::Range(c, min(c + tileWidth, hue.cols)));



         // tile processing here
         // .............
      }
   }



  cv::waitKey(0);
  return 0 ;


}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by StevenPuttemans
close date 2015-02-23 06:34:02.182516

Comments

have a look here in the function that I have submitted in another thread

theodore gravatar imagetheodore ( 2015-02-21 18:33:14 -0600 )edit
1

for (int r = 0; r < hue.rows - tileHeight; r += tileHeight) // atm, you're doing one too many, also you'd go over the image border.

berak gravatar imageberak ( 2015-02-22 02:21:23 -0600 )edit

@theodore, the number of rows of my image is primary, so the function is not working and asking for a different number of divisors, but i'm okay with a block that is smaller than the other, i can modify the algorithm to divide each image according to a different number, i need all my images to be divided into 16 tiles whether the tiles are even or not.

Apastrix gravatar imageApastrix ( 2015-02-22 13:14:30 -0600 )edit
1

@berak ,Your answer is perfect, it works for all image sizes, thank you!

Apastrix gravatar imageApastrix ( 2015-02-22 15:39:52 -0600 )edit