学习javacv入门示例2:访问Mat元素,对图像加盐处理

来源:互联网 发布:cda数据 编辑:程序博客网 时间:2024/06/05 02:11

Mat作为图像的逻辑结构,是二维多通道的。如何访问图像的元素,以及像素的各个通道,是本文的重点。

package com.linghushaoxia.javacv.chapter02;import static org.bytedeco.javacpp.opencv_imgcodecs.IMREAD_COLOR;import java.util.Random;import org.bytedeco.javacpp.indexer.UByteIndexer;import org.bytedeco.javacpp.opencv_core.Mat;import com.linghushaoxia.image.util.JavaCVUtil;/**功能说明:对图片添加噪点(加盐处理) * @author:linghushaoxia * @time:2016年4月19日下午8:39:09 * @version:1.0 * 为中国孱弱的技术, * 撑起一片自立自强的天空 */public class ImageSalt {    public static void main(String[] args) {//图片路径String filePath = "data/javacv/tower.JPG";// 以彩色模式读取图像Mat image = JavaCVUtil.imRead(filePath, IMREAD_COLOR);//原始图像JavaCVUtil.imShow(image,"原始图片");//对图像加盐Mat dest = salt(image, 2000);// 显示图像JavaCVUtil.imShow(dest, "加盐处理");    }   /**    *     * 功能说明:对图片加盐,添加噪点    * @param image    * 原始图片    * @param n    * 噪点数量    * @return Mat    * @time:2016年4月19日下午8:40:27    * @author:linghushaoxia    * @exception:    *    */    public static Mat salt(Mat image, int n) {// 随机数生成对象Random random = new Random();/** * 无符号字节索引,访问Mat结构的元素 * 访问Mat高效便捷 */UByteIndexer indexer = image.createIndexer();//图像通道int nbChannels = image.channels();//加盐数量for (int i = 0; i < n; i++) {    /**     * 获取随机行、列     * 噪点随机分布     */    int row = random.nextInt(image.rows());    int col = random.nextInt(image.cols());    //处理全部通道数据,均进行加盐,设置为最大值255    for (int channel = 0; channel < nbChannels; channel++) {indexer.put(row, col, channel, 255);    }}return image;    }}/*** 现实就是实现理想的过程。* */

原始图片


加盐处理


0 0
原创粉丝点击