1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include<opencv2/opencv.hpp>
#include <iostream>
#include <map>
using namespace cv;
using namespace std;
int main()
{
//Read an image and store it to a matrix.
Mat imageMatrix = imread("0.jpg");//<-- put your image here
if (imageMatrix.empty())
{
cerr<<"Can't read image !!\n";
return -1;
}
int imageWidth = imageMatrix.cols;
int imageHeight = imageMatrix.rows;
//Map of Vec3b vector.
map < int, vector<Vec3b> > mapOfVectors;
//Store the value of each row of pixels in each vector of the map.
for (int idRow = 0 ; idRow < imageHeight ; idRow++)
for (int idCol = 0 ; idCol < imageWidth ; idCol++)
mapOfVectors[idRow].push_back(imageMatrix.at<Vec3b>(idRow,idCol));
//Store the map in the a new matrix.
Mat newImageMatrix (imageHeight , imageWidth , CV_8UC3);
for( map<int, vector<Vec3b> >::iterator iter = mapOfVectors.begin(); iter != mapOfVectors.end(); ++iter )
{
int indexOfMapOfCevtors = (*iter).first;
vector<Vec3b> rowContents = (*iter).second;
for (int idCol = 0 ; idCol < imageWidth ; idCol++)
newImageMatrix.at<Vec3b>(indexOfMapOfCevtors,idCol) = rowContents[idCol];
}
//Display the new matrix (image)
namedWindow( "Image", CV_WINDOW_AUTOSIZE );
imshow("Image",newImageMatrix);
waitKey(0);
return 0;
} |
Partager