图像处理的镜像是什么

2015-04-08 09:41:06 Trent1985 阅读数 1328


[函数名称]

图像垂直镜像函数MirrorYProcess(WriteableBitmap src)

[函数代码]

       ///<summary>

       /// Vertical mirror process.

       ///</summary>

       ///<param name="src">Source image.</param>

       ///<returns></returns>

       publicstaticWriteableBitmap MirrorYProcess(WriteableBitmap src)////20垂直镜像

       {

           if(src!=null )

           {

           int w = src.PixelWidth;

           int h = src.PixelHeight;

           WriteableBitmap mirrorImage =newWriteableBitmap(w,h);

           byte[] temp = src.PixelBuffer.ToArray();

           byte[] tempMask =newbyte[w * h * 4];

           for (int i = 0; i < w; i++)

           {

               for (int j = 0; j < h; j++)

               {

                   tempMask[i * 4 + j * w * 4] = temp[i * 4 + (h - 1 - j) * w * 4];

                   tempMask[i * 4 + 1 + j * w * 4] = temp[i * 4 + 1 + (h - 1 - j) * w * 4];

                   tempMask[i * 4 + 2 + j * w * 4] = temp[i * 4 + 2 + (h - 1 - j) * w * 4];

                   tempMask[i * 4 + 3 + j * w * 4] = temp[i * 4 + 3 + (h - 1 - j) * w * 4];

               }

           }         

           Stream sTemp = mirrorImage.PixelBuffer.AsStream();

           sTemp.Seek(0, SeekOrigin.Begin);

           sTemp.Write(tempMask, 0, w * 4 * h);

           return mirrorImage;

           }

           else

           {

               returnnull;

           }            

       }

[图像效果]

2017-04-01 15:52:50 linshanxian 阅读数 3613

图像镜像分为水平镜像、垂直镜像和对角镜像三种:水平镜像是指将图像的左右部分以图像垂直中轴线为中心进行镜像对换;垂直镜像是将图像的上下两部分以图像水平中轴线为中心进行镜像对换;对角镜像是将图像以图像水平中轴线和垂直中轴线的交点为中心进行镜像对换,相当于将图像先后进行水平镜像和垂直镜像。下面具体阐述不同镜像的变换方法。

假设原图像的高度为h,宽度为w,变换后,图像的尺寸不变。那么原图像中(x0y0)经过水平镜像后坐标变为(w-1-x0y0)。用矩阵表示为:


逆变换为:



同理图像经过垂直镜像变换后的逆运算为


对角镜像的逆运算为:


主要代码如下:

void Mirror(const Mat &srcImage, Mat &dstImage, int flag)
{
    dstImage.create(srcImage.size(), srcImage.type());
    int nRowNum = srcImage.rows;
    int nColNum = srcImage.cols;
    switch(flag)
    {
    //水平镜像
    case 1:
        for(int i = 0; i < nRowNum; i++)
        {
            for(int j = 0; j < nColNum; j++)
            {
                dstImage.at<Vec3b>(i, j) = srcImage.at<Vec3b>(nRowNum - i - 1, j);
            }
        }
        break;
    //垂直镜像
    case 2:
        for(int i = 0; i < nRowNum; i++)
        {
            for(int j = 0; j < nColNum; j++)
            {
                dstImage.at<Vec3b>(i, j) = srcImage.at<Vec3b>(i, nColNum - j - 1);
            }
        }
        break;
    //对角镜像
    case 3:
        for(int i = 0; i < nRowNum; i++)
        {
            for(int j = 0; j < nColNum; j++)
            {
                dstImage.at<Vec3b>(i, j) = srcImage.at<Vec3b>(nRowNum - i - 1, nColNum - j - 1);
            }
        }
    }
}


2017-05-31 22:27:04 sinat_36246371 阅读数 2695

水平镜像变换,也就是把图像的像素点按照垂直中线做调换。

代码实现也很简单:

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageMirror {
    public static void main(String[] args) throws IOException{
        File file = null;
        BufferedImage image = null;

        try {
            file = new File("E:\\in.jpg");
            image = ImageIO.read(file);

            int width = image.getWidth();
            int height = image.getHeight();

            for (int j = 0; j < height; j++) {
                int l = 0, r = width - 1;
                while (l < r) {
                    int pl = image.getRGB(l, j);
                    int pr = image.getRGB(r, j);

                    image.setRGB(l, j, pr);
                    image.setRGB(r, j, pl);

                    l++;
                    r--;
                }
            }

            file = new File("E:\\out.jpg");
            ImageIO.write(image, "jpg", file);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

输入:


这里写图片描述

输出:


这里写图片描述

2014-04-11 14:32:17 flyxkh 阅读数 938
简单的实现了三通道,1通道和别的通道类似
bool Mirror(Mat &img)
{
	if(!img.data)
		return false;
	unsigned char *pSrc = NULL,*pDst = NULL;
	int w = img.cols;
	int h = img.rows;
	int cn = img.channels();

	Mat img_mirror = img.clone();
	int wdt = (w-1)*cn;
	pSrc = img_mirror.data + wdt;
	pDst = img.data;
	switch (cn)
	{
	case 3:
		for (int y=0;y<h;y++)
		{
			for (int x=0;x<w*cn;x+=3)
			{
				*(pDst+x+0) = *(pSrc -x + 0);
				*(pDst+x+1) = *(pSrc -x + 1);
				*(pDst+x+2) = *(pSrc -x + 2);
			}
			pDst += w*cn;
			pSrc += w*cn;
		}
		break;
	case 1:

		break;
	default:
		break;
	}

	return true;
}

2016-05-19 20:22:28 yang332233 阅读数 2275
#include "opencv2/core/core.hpp"
#include"opencv2/highgui/highgui.hpp"
#include<opencv2/opencv.hpp>
#include "iostream"
using namespace std;
using namespace cv;

void OnMirror_X(Mat img,Mat &OutImg)
{
	int Width=img.cols;
	int Height=img.rows;
	OutImg.create(Height,Width,img.type());

	for(int i=0;i<Height;i++)
		for(int j=0;j<Width/2;j++)
		{
			if(img.channels()==1)
			{

				OutImg.at<uchar>(i,j)=img.at<uchar>(i,Width-j-1);
				OutImg.at<uchar>(i,(Width-j-1))=img.at<uchar>(i,j);
			}
			else if(img.channels()==3)
			{
				OutImg.at<Vec3b>(i,j)=img.at<Vec3b>(i,Width-j-1);
				OutImg.at<Vec3b>(i,Width-j-1)=img.at<Vec3b>(i,j);
			}
		}
}

void OnMirror_Y(Mat img,Mat &OutImg)
{

	int Width=img.cols;
	int Height=img.rows;
	OutImg.create(Height,Width,img.type());
	for(int i=0;i<Height/2;i++)
		for(int j=0;j<Width;j++)
		{
			if(img.channels()==1)
			{
				OutImg.at<uchar>(i,j)=img.at<uchar>(Height-i-1,j);  //一开始没加-1  内存老报错!!!~~~  细心啊
				OutImg.at<uchar>(Height-i-1,j)=img.at<uchar>(i,j);

			}
			else if(img.channels()==3)
			{
				OutImg.at<Vec3b>(i,j)=img.at<Vec3b>(Height-i-1,j);
				OutImg.at<Vec3b>(Height-i-1,j)=img.at<Vec3b>(i,j);
			}
		}}
<pre name="code" class="cpp">void Transpose(Mat img,Mat &OutImg) // 图像转置
{
	int Width=img.cols;
	int Height=img.rows;

	OutImg.create(Width,Height,img.type());//注意这里的长、宽

	for(int i=0;i<Width;i++)  //注意这里的顺序
		for(int j=0;j<Height;j++)
		{
			if(img.channels()==1)
			{
				OutImg.at<uchar>(i,j)=img.at<uchar>(j,i);
			}
			else if(img.channels()==3)
			{
				OutImg.at<Vec3b>(i,j)=img.at<Vec3b>(j,i);
			}
		}
}



void main()
{
	Mat SrcImg=imread("C:\\Users\\Administrator\\Desktop\\工作\\testp\\01.jpg");
	if(!SrcImg.data)
		cout<<"读取图片错误\n";

	Mat ResultImg1,ResultImg2;
	OnMirror_X(SrcImg,ResultImg1);
	OnMirror_Y(SrcImg,ResultImg2);

	imshow("X",ResultImg1);
	imshow("Y",ResultImg2);
	imshow("src",SrcImg);
	waitKey(0);

}