Block process an image? [closed]
I want to :
- Read an image.
- Convert it to HSV and take only the Hue part.
- Divide the Hue plane into 16 blocks.
- Calculate for each block the Histogram and extract a feature based on it (maybe the dominant colour).
- Build a feature Vector.
- 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 ;
}
have a look here in the function that I have submitted in another thread
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.
@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.
@berak ,Your answer is perfect, it works for all image sizes, thank you!