python opencv3图像膨胀和腐蚀

#encoding=utf-8
import cv2
import numpy as np
img = cv2.imread("E:/1.jpg", -1)
#cv2.namedWindow('img')
cv2.imshow('img', img)
cv2.waitKey(0)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3, 3)) 
eroded = cv2.erode(img,kernel)
dilated = cv2.dilate(img,kernel)
cv2.imwrite("E:/11.jpg",eroded)
cv2.imwrite("E:/22.jpg",dilated)
cv2.destroyAllWindows();

python安装opencv3步骤:

1、用virtualenv命令创建虚拟目录opencv3

2、下载opencv3.2(或其他版本)解压后,找到cv2.pyd(注意根据系统平台选择x86或x64目录下的文件),将这个文件拷贝到python虚拟目录opencv3\lib\site-packages下

3、在opencv3虚拟目录下安装numpy和matplotlib库

4、import cv2进行测试,如失败则升级numpy:pip install numpy --upgrade


C# Emgu库:

private void button1_Click(object sender, EventArgs e) 
{
  //var img = Emgu.CV.Image.FromIplImagePtr(_fileName); var img = new Emgu.CV.Image(_fileName); 
  pictureBox3.Image = img.ToBitmap(); 
  var dstImg = new Emgu.CV.Image(img.Width, img.Height);
  //(IInputArray)Emgu.CV.CvInvoke.cvCreateImage(Emgu.CV.CvInvoke.cvGetSize(img), IplDepth.IplDepth_8U, 1); 
  Mat kenel = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Cross, new Size(2, 2), new Point(1, 1)); 
  Emgu.CV.CvInvoke.Dilate(img, dstImg, kenel, new Point(1, 1), 1, BorderType.Default, new MCvScalar(0.2));
  pictureBox2.Image = dstImg.Bitmap; 
  dstImg.Save("D:\\1.png"); }

你可能感兴趣的:(python opencv3图像膨胀和腐蚀)