图像剪切C ++

时间:2017-10-29 10:01:53

标签: c++ opencv image-processing

我正在尝试使用OpenCV沿X轴剪切图像以加载图像,并使用以下算法剪切图像:x'= x + y·Bx,但由于某种原因,我最终得到了以下剪切:

我的源代码如下所示:

#include "stdafx.h"
#include "opencv2\opencv.hpp"

using namespace std;
using namespace cv;

int main()
{
    Mat src = imread("B2DBy.jpg", 1);

    if (src.empty())
        cout << "Error: Loading image" << endl;

    int r1, c1; // tranformed point
    int rows, cols; // original image rows and columns
    rows = src.rows;
    cols = src.cols;

    float Bx = 2; // amount of shearing in x-axis
    float By = 0; // amount of shearing in y-axis

    int maxXOffset = abs(cols * Bx);
    int maxYOffset = abs(rows * By);

    Mat out = Mat::ones(src.rows + maxYOffset, src.cols + maxXOffset, src.type()); // create output image to be the same as the source

    for (int r = 0; r < out.rows; r++) // loop through the image
    {
        for (int c = 0; c < out.cols; c++)
        {
            r1 = r + c * By - maxYOffset; // map old point to new
            c1 = r * Bx + c - maxXOffset;

            if (r1 >= 0 && r1 <= out.rows && c1 >= 0 && c1 <= out.cols) // check if the point is within the boundaries
            {
                out.at<uchar>(r, c) = src.at<uchar>(r1, c1); // set value
            }

        }
    }

    namedWindow("Source image", CV_WINDOW_AUTOSIZE);
    namedWindow("Rotated image", CV_WINDOW_AUTOSIZE);
    imshow("Source image", src);
    imshow("Rotated image", out);

    waitKey(0);

    return 0;
}

修改

我自己修好了。 不需要减去偏移量。下面是更新的源代码:

Mat forward(Mat img) {

Mat umg = img;
int y1, x1; // tranformed point
int rows, cols; // original image rows and columns
rows = umg.rows;
cols = umg.cols;

float Bx = 0.7; // amount of shearing in x-axis
float By = 0; // amount of shearing in y-axis

int maxXOffset = abs(rows * Bx);
int maxYOffset = abs(cols * By);

Mat out = Mat::ones(rows + maxYOffset, cols + maxXOffset, umg.type()); // create output image to be the same as the source

for (int y = 0; y < rows; y++) // loop through the image
{
    for (int x = 0; x < cols; x++)
    {
        y1 = y + x * By; // map old point to new
        x1 = y * Bx + x;

        out.at<uchar>(y1, x1) = umg.at<uchar>(y, x); // set value
    }
}

return out;
}

Mat backwards(Mat img) {

Mat umg = img;
int y1, x1; // tranformed point
int rows, cols; // original image rows and columns
rows = umg.rows;
cols = umg.cols;

float Bx = 0.7; // amount of shearing in x-axis
float By = 0; // amount of shearing in y-axis

int maxXOffset = abs(rows * Bx);
int maxYOffset = abs(cols * By);

Mat out = Mat::ones(rows + maxYOffset, cols + maxXOffset, umg.type()); // create output image to be the same as the source

for (int y = 0; y < rows; y++) // loop through the image
{
    for (int x = 0; x < cols; x++)
    {
        //y1 = y + x * By; // map old point to new
        //x1 = y * Bx + x;

        y1 = (1 / (1 - Bx*By)) * (y + x * By);
        x1 = (1 / (1 - Bx*By)) * (y * Bx + x);

        out.at<uchar>(y1, x1) = umg.at<uchar>(y, x); // set value
    }
}

return out;
}

int main()
{
Mat src = imread("B2DBy.jpg", 0);

if (src.empty())
    cout << "Error: Loading image" << endl;

Mat forwards = forward(src);
Mat back = backwards(src);

namedWindow("Source image", CV_WINDOW_NORMAL);
imshow("Source image", src);
imshow("back", back);
imshow("forward image", forwards);

waitKey(0);

return 0;
}

1 个答案:

答案 0 :(得分:1)

我找到了一些时间来解决这个问题。 现在我理解你试图通过偏移计算实现的目标,但我不确定你的是否正确。

如果需要,只需将所有f = open('abc.txt','r') while(True): try: line = next(f) print(line,end='') except: break 更改为cv::Vec3bunsigned char并加载为灰度。

请尝试使用此代码,也许您会发现错误:

uchar

给出3个样本行的这些输出:

enter image description here

enter image description here

enter image description here