通道的分离与合并
class Program { static void Main(String[] args) { Mat img = CvInvoke.Imread(@"C:UsersdellPicturesmach.jpg"); Mat pic = new Mat(); int ch=img.NumberOfChannels; VectorOfMat dst = new VectorOfMat(ch); CvInvoke.Split(img,dst); CvInvoke.Imshow("hello", img); Mat blue = dst[0]; Mat green = dst[1]; Mat red = dst[2]; CvInvoke.Threshold(blue, blue, 200, 255, ThresholdType.Binary); CvInvoke.Threshold(green, green, 200, 255, ThresholdType.Binary); CvInvoke.Threshold(red, red, 200, 255, ThresholdType.Binary); CvInvoke.Merge(dst, pic); CvInvoke.Imshow("b", blue); CvInvoke.Imshow("g", green); CvInvoke.Imshow("r", red); CvInvoke.Imshow("m", pic); CvInvoke.WaitKey(0); } }
效果如下:
ROI
class Program { static void Main(String[] args) { Mat img = CvInvoke.Imread(@"C:UsersdellPicturesmach.jpg"); Mat logo = CvInvoke.Imread(@"C:UsersdellPicturesopencv.jpg"); Mat ROI = new Mat(img, new Rectangle(20, 20, logo.Cols, logo.Rows)); logo.CopyTo(ROI); CvInvoke.Imshow("roi", img); CvInvoke.WaitKey(0); } }
MASK掩码
static void Main(String[] args) { Mat img = CvInvoke.Imread(@"C:UsersdellPicturesmach.jpg"); Mat logo = CvInvoke.Imread(@"C:UsersdellPicturesopencv.jpg"); Mat mask = CvInvoke.Imread(@"C:UsersdellPicturesopencv.jpg", 0); CvInvoke.BitwiseNot(mask, mask);//图像取反,白色变黑色 CvInvoke.Imshow("mask", mask); CvInvoke.Threshold(mask, mask, 100, 255, ThresholdType.Binary); CvInvoke.Imshow("threshold", mask); Mat roi = new Mat(img, new Rectangle(20, 20, logo.Cols, logo.Rows)); logo.CopyTo(roi, mask); CvInvoke.Imshow("img", img); CvInvoke.WaitKey(0); }