c# - 使用 WarpAffine() 将 Emgu.CV.Image 向右移动
问题描述
我的目标是,将图像向右移动 x 个像素。我想这可以通过使用 WarpAffine 来实现。这至少是我的研究告诉我的。我使用了多种不同的方法,例如:
CvInvoke.WarpAffine(realImage, transformedImage, transformationMatrix, new Size(realImage.Size);
//or while m is the Mat used to create realImage
transImage.WarpAffine(m,Emgu.CV.CvEnum.Inter.Area,Emgu.CV.CvEnum.Warp.Default,Emgu.CV.CvEnum.BorderType.Default,new Gray());
我得到了例外:
Exception thrown: 'Emgu.CV.Util.CvException' in Emgu.CV.Platform.NetStandard.dll
[ WARN:0] global E:\bb\cv_x86\build\opencv\modules\videoio\src\cap_msmf.cpp (436) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
我想我用错了,但网上没有合适的例子可供我学习。有没有人有干净的方式向我解释?
先感谢您!
解决方案
如果您将像素 x 向右移动,我假设左侧会有黑色的空像素?如果是这样,您可以创建一个 ROI 并切断右侧的一些像素,因为您将所有像素向右移动,然后将图像复制到另一个图像上。
//The image that you want to shift pixels with
Image<Bgr, byte> inputImage = new Image<Bgr, byte>(1000, 1000);
//The output image
Image<Bgr, byte> image = new Image<Bgr, byte>(990, 1000);
//Create the roi, with 10 pixels cut off from the right side because of the shift
inputImage.ROI = new Rectangle(0, 0, inputImage.Width - 10, inputImage.Height);
inputImage.CopyTo(image);
CvInvoke.ImShow("The Output", image);
CvInvoke.WaitKey(0);
编辑
现在假设您也想在图像的左侧保留黑色条纹。这样做与上面的代码非常相似,但只做了一些修改。
//The image that you want to shift pixels with
Image<Bgr, byte> inputImage = new Image<Bgr, byte>(1000, 1000);
//The output image
Image<Bgr, byte> image = new Image<Bgr, byte>(1000, 1000);
//Create the roi, with 10 pixels cut off from the right side because of the shift
inputImage.ROI = new Rectangle(0, 0, inputImage.Width - 10, inputImage.Height);
//The image we want to shift, the same one we created a ROI with,
//has the dimensions 990 X 1000 and the output image has
//dimensions of 1000 x 1000. Unfortunately, in order to paste
//an image onto another image, they need to be the same dimensions.
//How do we do this? We must create an ROI with the output image
//that has the same dimensions as the input image.
image.ROI = new Rectangle(10, 0, image.Width, image.Height);
//Now we can past the image onto the output because the dimensions match
inputImage.CopyTo(image);
//Inorder to make our output seem normal, we must empty the ROI of the output image
image.ROI = Rectangle.Empty;
CvInvoke.ImShow("The Output", image);
CvInvoke.WaitKey(0);
推荐阅读
- vue.js - 让当前页面在菜单栏上以粗体显示
- javascript - Let definition are not supported by current JavaScript version in PhpStorm
- excel - 提取具有特定文本的两行之间的值(动态范围)
- tensorflow - 是否可以从 tflite 模型中提取经过训练的类名?
- python - 如何使用“开放”逻辑在 Python 中定义函数?
- node.js - 如何在express中实现resolveType函数/初始化graphQL服务器
- android - 如何从 Play 结算库中获取货币符号
- sql - 自定义 SQL 锁定方案的安全性
- php - Elasticsearch 抛出 http 方法异常。Platform.sh 上的 Symfony 4.4
- python - 使用 numpy getfromtxt 从多个文件循环导入数据