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 55 56
|
#include
#include
#include
using namespace std;
using namespace cv;
void Scale(float Sx, float Sy, int imageWidth, int imageHeight)
// The scaling function, taking four values from the main and creating the scaled image.
{
Mat imageScaled(imageHeight * Sy, imageWidth * Sx, 0);
Mat imageOriginal = imread("picture.jpg", CV_LOAD_IMAGE_COLOR);
cvtColor(imageOriginal, imageOriginal, CV_BGR2GRAY);
for(int X = 0; X < imageWidth; X++){
for(int Y = 0; Y < imageHeight; Y++){
int OutputWidth = X * Sx;
int OutputHeight = Y * Sy;
imageScaled.at(OutputHeight, OutputWidth) = imageOriginal.at(Y, X);
}
}
imshow("Original Input Image", imageOriginal);
imshow("Scaled Image", imageScaled);
}
int main()
{
float Sx;
float Sy;
cout << "Forward Mapped Image Scaling" << endl << endl;
cout << "Input file: ";
Mat image = imread("picture.jpg", CV_LOAD_IMAGE_COLOR);
if(! image.data || image.empty()){
cout << "..." << endl << endl << "Error!" << endl << "Could not open or find picture.jpg." << endl;
}
else{
cvtColor(image, image, CV_BGR2GRAY);
cout << "picture.jpg" << endl;
cout << "Image width: " << image.cols << endl;
cout << "Image height: " << image.rows << endl << endl;
cout << "Set the factors for the image's horizontal and vertical scaling." << endl;
cout << "X axis (in %): ";
cin >> Sx;
cout << "Y axis (in %): ";
cin >> Sy;
Sx = Sx / 100;
Sy = Sy / 100;
cout << endl << "Applying the factors to the image size:" << endl;
cout << "Scaled image width: " << image.cols * Sx << endl;
cout << "Scaled image height: " << image.rows * Sy << endl;
Scale(Sx, Sy, image.cols, image.rows);
waitKey(0);
return 0;
}
}
|