Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 图像剪切C++;_C++_Opencv_Image Processing - Fatal编程技术网

C++ 图像剪切C++;

C++ 图像剪切C++;,c++,opencv,image-processing,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())

我尝试使用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;
}
#包括“stdafx.h”
#包括“opencv2\opencv.hpp”
使用名称空间std;
使用名称空间cv;
int main()
{
Mat src=imread(“B2DBy.jpg”,1);
if(src.empty())

我找时间做这件事。 现在我明白了你们试图通过偏移量计算来实现什么,但我不确定你们的计算是否正确

只要将所有的
cv::Vec3b
更改为
unsigned char
uchar
,并按灰度加载即可

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

// no interpolation yet
// cv::Vec3b only
cv::Mat shear(const cv::Mat & input, float Bx, float By)
{
    if (Bx*By == 1)
    {
        throw("Shearing: Bx*By==1 is forbidden");
    }


    if (input.type() != CV_8UC3) return cv::Mat();

    // shearing:
    // x'=x+y·Bx
    // y'=y+x*By

    // shear the extreme positions to find out new image size:
    std::vector<cv::Point2f> extremePoints;
    extremePoints.push_back(cv::Point2f(0, 0));
    extremePoints.push_back(cv::Point2f(input.cols, 0));
    extremePoints.push_back(cv::Point2f(input.cols, input.rows));
    extremePoints.push_back(cv::Point2f(0, input.rows));

    for (unsigned int i = 0; i < extremePoints.size(); ++i)
    {
        cv::Point2f & pt = extremePoints[i];
        pt = cv::Point2f(pt.x + pt.y*Bx, pt.y + pt.x*By);
    }

    cv::Rect offsets = cv::boundingRect(extremePoints);

    cv::Point2f offset = -offsets.tl();
    cv::Size resultSize = offsets.size();

    cv::Mat shearedImage = cv::Mat::zeros(resultSize, input.type()); // every pixel here is implicitely shifted by "offset"

    // perform the shearing by back-transformation
    for (int j = 0; j < shearedImage.rows; ++j)
    {

        for (int i = 0; i < shearedImage.cols; ++i)
        {
            cv::Point2f pp(i, j);

            pp = pp - offset; // go back to original coordinate system

            // go back to original pixel:
            // x'=x+y·Bx
            // y'=y+x*By
            //   y = y'-x*By
            //     x = x' -(y'-x*By)*Bx 
            //     x = +x*By*Bx - y'*Bx +x'
            //     x*(1-By*Bx) = -y'*Bx +x'
            //     x = (-y'*Bx +x')/(1-By*Bx)

            cv::Point2f p;
            p.x = (-pp.y*Bx + pp.x) / (1 - By*Bx);
            p.y = pp.y - p.x*By;

            if ((p.x >= 0 && p.x < input.cols) && (p.y >= 0 && p.y < input.rows))
            {
                // TODO: interpolate, if wanted (p is floating point precision and can be placed between two pixels)!
                shearedImage.at<cv::Vec3b>(j, i) = input.at<cv::Vec3b>(p);
            }
        }
    }

    return shearedImage;
}


int main(int argc, char* argv[])
{
    cv::Mat input = cv::imread("C:/StackOverflow/Input/Lenna.png");

    cv::Mat output = shear(input, 0.7, 0);
    //cv::Mat output = shear(input, -0.7, 0);
    //cv::Mat output = shear(input, 0, 0.7);

    cv::imshow("input", input);
    cv::imshow("output", output);

    cv::waitKey(0);
    return 0;
}
//还没有插值
//cv::仅限Vec3b
cv::垫剪(常数cv::垫和输入、浮动Bx、浮动Bx)
{
如果(Bx*By==1)
{
投掷(“禁止剪切:Bx*By==1”);
}
if(input.type()!=CV_8UC3)返回CV::Mat();
//剪切:
//x'=x+y·Bx
//y'=y+x*By
//剪切极端位置以找出新的图像大小:
std::向量极值点;
极值点。向后推(cv::Point2f(0,0));
extremePoints.push_back(cv::Point2f(input.cols,0));
extremePoints.push_back(cv::Point2f(input.cols,input.rows));
extremePoints.push_back(cv::Point2f(0,input.rows));
for(无符号整数i=0;i=0&&p.x=0&&p.y
给我3条采样线的输出:


你的结果怎么了?我没有得到完整的图像。它以一种奇怪的方式剪切了图像!in.Mat src=imread(“B2DBy.jpg”,1);1是“灰度”的代码吗?剪切是如何定义的?为什么不使用单应变形来计算剪切?通过在最内层的if子句中添加对r1-r的测试,你能找出应用于图像的最大实际偏移量吗?我将通过计算偏移量来实现它,而不是在第一步中计算绝对位置。我发现很难获得剪切算法,并且我很瘦k r1和c1计算以及最大偏移量计算中也存在误差。
// no interpolation yet
// cv::Vec3b only
cv::Mat shear(const cv::Mat & input, float Bx, float By)
{
    if (Bx*By == 1)
    {
        throw("Shearing: Bx*By==1 is forbidden");
    }


    if (input.type() != CV_8UC3) return cv::Mat();

    // shearing:
    // x'=x+y·Bx
    // y'=y+x*By

    // shear the extreme positions to find out new image size:
    std::vector<cv::Point2f> extremePoints;
    extremePoints.push_back(cv::Point2f(0, 0));
    extremePoints.push_back(cv::Point2f(input.cols, 0));
    extremePoints.push_back(cv::Point2f(input.cols, input.rows));
    extremePoints.push_back(cv::Point2f(0, input.rows));

    for (unsigned int i = 0; i < extremePoints.size(); ++i)
    {
        cv::Point2f & pt = extremePoints[i];
        pt = cv::Point2f(pt.x + pt.y*Bx, pt.y + pt.x*By);
    }

    cv::Rect offsets = cv::boundingRect(extremePoints);

    cv::Point2f offset = -offsets.tl();
    cv::Size resultSize = offsets.size();

    cv::Mat shearedImage = cv::Mat::zeros(resultSize, input.type()); // every pixel here is implicitely shifted by "offset"

    // perform the shearing by back-transformation
    for (int j = 0; j < shearedImage.rows; ++j)
    {

        for (int i = 0; i < shearedImage.cols; ++i)
        {
            cv::Point2f pp(i, j);

            pp = pp - offset; // go back to original coordinate system

            // go back to original pixel:
            // x'=x+y·Bx
            // y'=y+x*By
            //   y = y'-x*By
            //     x = x' -(y'-x*By)*Bx 
            //     x = +x*By*Bx - y'*Bx +x'
            //     x*(1-By*Bx) = -y'*Bx +x'
            //     x = (-y'*Bx +x')/(1-By*Bx)

            cv::Point2f p;
            p.x = (-pp.y*Bx + pp.x) / (1 - By*Bx);
            p.y = pp.y - p.x*By;

            if ((p.x >= 0 && p.x < input.cols) && (p.y >= 0 && p.y < input.rows))
            {
                // TODO: interpolate, if wanted (p is floating point precision and can be placed between two pixels)!
                shearedImage.at<cv::Vec3b>(j, i) = input.at<cv::Vec3b>(p);
            }
        }
    }

    return shearedImage;
}


int main(int argc, char* argv[])
{
    cv::Mat input = cv::imread("C:/StackOverflow/Input/Lenna.png");

    cv::Mat output = shear(input, 0.7, 0);
    //cv::Mat output = shear(input, -0.7, 0);
    //cv::Mat output = shear(input, 0, 0.7);

    cv::imshow("input", input);
    cv::imshow("output", output);

    cv::waitKey(0);
    return 0;
}