refs:https://docs.opencv.org/4.3.0/d5/d98/tutorial_mat_operations.html

目录

Basic operations with images

Java

Python

Memory management and reference counting

Visualizing images

Java

C++

Python


Basic operations with images

In order to get pixel intensity value, you have to know the type of an image and the number of channels. Here is an example for a single channel grey scale image (type 8UC1) and pixel coordinates x and y:

 Scalar intensity = img.at<uchar>(y, x);Scalar intensity = img.at<uchar>(Point(x, y));

y,x  x,y顺序不同

Now let us consider a 3 channel image with BGR color ordering (the default format returned by imread):

  Vec3b intensity = img.at<Vec3b>(y, x);uchar blue = intensity.val[0];uchar green = intensity.val[1];uchar red = intensity.val[2];

Java

byte[] imgData = new byte[(int) (img.total() * img.channels())];img.get(0, 0, imgData);byte intensity = imgData[y * img.cols() + x];

Python

_blue = img[y,x,0]_green = img[y,x,1]_red = img[y,x,2]

Memory management and reference counting

Mat is a structure that keeps matrix/image characteristics (rows and columns number, data type etc) and a pointer to data. So nothing prevents us from having several instances of Mat corresponding to the same data. A Mat keeps a reference count that tells if data has to be deallocated when a particular instance of Mat is destroyed.

Visualizing images

It is very useful to see intermediate results of your algorithm during development process. OpenCV provides a convenient way of visualizing images. A 8U image can be shown using:

  Mat img = imread("image.jpg");namedWindow("image", WINDOW_AUTOSIZE);imshow("image", img);waitKey();

A call to waitKey() starts a message passing cycle that waits for a key stroke in the "image" window. A 32F image needs to be converted to 8U type. For example:

Java

package com.company;import java.util.Scanner;import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;public class Main {static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }public static void main(String[] args) {System.out.println("Welcome to OpenCV " + Core.VERSION);
//            String filename = "././wolf.jpg";String filename = "Grass.jpg";Mat src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);Imgproc.pyrDown(src, src);Mat grey = new Mat();Imgproc.cvtColor(src, grey, Imgproc.COLOR_BGR2GRAY);Mat sobelx = new Mat();Imgproc.Sobel(grey, sobelx, CvType.CV_32F, 1, 0);Core.MinMaxLocResult res = Core.minMaxLoc(sobelx); // find minimum and maximum intensitiesMat draw = new Mat();double maxVal = res.maxVal, minVal = res.minVal;sobelx.convertTo(draw, CvType.CV_8U, 255.0 / (maxVal - minVal), -minVal * 255.0 / (maxVal - minVal));HighGui.imshow( "Input", src );HighGui.imshow("sobel-convert", draw);HighGui.waitKey();System.exit(0);}}

C++

#include <iostream>
#include <opencv.hpp>
#include <ctime>using namespace cv;
using namespace std;void main()
{Mat image_grass = imread("Grass.jpg", IMREAD_COLOR);//Mat image_grass = imread("Grass.jpg", IMREAD_GRAYSCALE);if (!image_grass.data){printf("No data\n");return;}pyrDown(image_grass, image_grass, Size(image_grass.cols / 2, image_grass.rows / 2));imshow("grass", image_grass);Mat grey;cvtColor(image_grass, grey, COLOR_BGR2GRAY);Mat sobelx;Sobel(grey, sobelx, CV_32F, 1, 0);imshow("sobel", sobelx);double minVal = 0.0f, maxVal = 0.0f;minMaxLoc(sobelx, &minVal, &maxVal);Mat draw;sobelx.convertTo(draw, CV_8U, 255.0 / 50, 0.0f);//sobelx.convertTo(draw, CV_8U, 0.0f, -minVal*255.0 / (maxVal - minVal));//sobelx.convertTo(draw, CV_8U, 255.0 / (maxVal - minVal), -minVal*255.0 / (maxVal - minVal));imshow("image", draw);waitKey();return;
}

Python

   img = cv.imread('image.jpg')grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)sobelx = cv.Sobel(grey, cv.CV_32F, 1, 0)# find minimum and maximum intensitiesminVal = np.amin(sobelx)maxVal = np.amax(sobelx)draw = cv.convertScaleAbs(sobelx, alpha=255.0/(maxVal - minVal), beta=-minVal * 255.0/(maxVal - minVal))cv.namedWindow('image', cv.WINDOW_AUTOSIZE)cv.imshow('image', draw)cv.waitKey()

np.amin/max

convertScaleAbs

from __future__ import print_function
import sys
import timeimport numpy as np
import cv2 as cvdef main():image = cv.imread("Grass.jpg", cv.IMREAD_COLOR)if image is None:print("No data")print("水光潋滟晴方好,山色空蒙雨亦奇。")else:print("欲把西湖比西子,淡妆浓抹总相宜。")src = cv.pyrDown(image)cv.imshow("src image", src);grey = cv.cvtColor(src, cv.COLOR_BGR2GRAY)cv.imshow("grey", grey);sobelx = cv.Sobel(grey, cv.CV_32F, 1, 0)# find minimum and maximum intensitiesminVal = np.amin(sobelx)maxVal = np.amax(sobelx)draw = cv.convertScaleAbs(sobelx, alpha=255.0 / (maxVal - minVal), beta=-minVal * 255.0 / (maxVal - minVal))cv.namedWindow('image', cv.WINDOW_AUTOSIZE)cv.imshow('image', draw)cv.waitKey()cv.destroyAllWindows()def readImageTest():image = cv.imread("Grass.jpg", cv.IMREAD_COLOR)if image is not None:pyrImage = cv.pyrDown(image)cv.namedWindow("Input", cv.WINDOW_AUTOSIZE)cv.imshow("Input", pyrImage)cv.waitKey()else:print("No data")if __name__ == "__main__":# readImageTest()main()

Improving Opencv 4: The Core Functionality :Operations with images相关推荐

  1. opencv回顾之Core module

    该篇围绕Core Functionality模块进行展开 该模块的主要作用是成为构建opencv更多高级功能的基础核心层. Mat基础图像存储数据结构 将Mat对象赋值给其他Mat变量将会共享一个地址 ...

  2. OpenCV 基本阈值操作Thresholding Operations

    OpenCV 基本阈值操作Thresholding Operations 基本阈值操作Thresholding Operations 目标 门槛? 阈值类型 阈值二进制 阈值二进制,反转 截短 阈值为 ...

  3. OpenCV 之 Mat 类

    数字图像可看作一个数值矩阵, 其中的每个元素代表一个像素点,如下图所示: OpenCV 中,用 Mat 来表示该数值矩阵,它是很关键的一种数据结构,因为 OpenCV 中的大部分函数都和 Mat 有关 ...

  4. 使用 OpenCV4 和 C++ 构建计算机视觉项目:1~5

    原文:Building Computer Vision Projects with OpenCV 4 and C++ 协议:CC BY-NC-SA 4.0 译者:飞龙 本文来自[ApacheCN 计算 ...

  5. OpenCV 1.x 2.x 编程简介(矩阵/图像/视频的基本读写操作)

    OpenCV 编程简介(矩阵/图像/视频的基本读写操作) Introduction to programming with OpenCV OpenCV编程简介 作者: Gady Agam Depart ...

  6. OpenCV 编程简单介绍(矩阵/图像/视频的基本读写操作)

    PS. 因为csdn博客文章长度有限制,本文有部分内容被截掉了. 在OpenCV中文站点的wiki上有可读性更好.而且是完整的版本号,欢迎浏览. OpenCV Wiki :<OpenCV 编程简 ...

  7. OpenCV在线文档目录翻译(一)

    英文地址:http://docs.opencv.org/ 对OpenCV的Online Documentation 目录文档进行了翻译整理. OpenCV API Reference 目录:Conte ...

  8. 【opencv 官方教程】翻译2 核心功能 中 图像操作 线性变换 亮度调整 简单绘图

    核心功能 The Core Functionality (core module) Here you will learn the about the basic building blocks of ...

  9. opencv 4.0.0教程系列01-core模块简介

    The Core Functionality (core module) {#tutorial_table_of_content_core} Here you will learn the about ...

  10. opencv超好学习资料

    日志 返回日志列表 opencv超好学习资料 2010-9-25 13:40阅读(0) 赞赞赞赞 转载 分享 评论 复制地址 举报 编辑 上一篇 |下一篇:opencv安装方法 w... v首页 资讯 ...

最新文章

  1. 海尔推“智能服务”标准 家电产业迎来“互联网+”
  2. 基于Redis实现分布式锁
  3. 谈谈C#反射(Reflection)
  4. Ubuntu 下JDK安装
  5. 测试面试题,自动化测试与性能测试篇(附答案)
  6. BarTender怎么打印公式化的三列标签
  7. android之隐藏状态栏、图标、布局
  8. 软件测试用mac还是windows,Boot Camp还是虚拟机?Mac+Win实测
  9. 【数据库原理实验(openGauss)】实验报告
  10. STL-关联式容器 map
  11. iOS开发:报错The sandbox is not in sync with the Podfile.lock. Run ‘pod install‘ …的解决方法
  12. 16qam星座图 matlab,16QAM星座图
  13. Visual Basic Script 程序参考手册-学习第4天:数组列表及Msgbox函数
  14. 轻量级网络:ResNeXt
  15. Janus的STUN原理与抓包分析
  16. 电脑开机输完密码就黑屏的解决历程
  17. 201217,成交量异动检测
  18. freeswitch对接ims
  19. 芯片服务器培训,服务器介绍及培训.ppt
  20. Revit创建装饰纹路柱及CAD生成柱

热门文章

  1. 初探线程之线程竞争及故障
  2. hashmap为什么8转成红黑树_看了两天HashMap源码,终于把红黑树插入平衡规则搞懂了...
  3. python 移动平均线_如何使用NumPy计算移动平均线?
  4. java中时时检查代码中变量的值_如何调试JDK源代码并查看局部变量值
  5. 我的世界如何开直连服务器,我的世界服务器如何连接 连接服务器教程
  6. 在公司网络中如何手动为apt-get设置代理
  7. 29.AngularJS 简介
  8. Android必知必会-使用Intent打开第三方应用及验证可用性
  9. 《2017微信春节数据报告》出炉 初一到初五微信红包收发总量达到460亿个
  10. 【云栖精选】6篇深度!解除MySQL数据同步疑惑+Docker技术示例