qcode
  • 创作工场专业版,一问多答!更智能、更完整、更原创
  • 这是一个超强内容创作平台,它会写文章、做设计、绘图、敲代码,有问必答!
🔥点击使用-高级专业版🔥

Requirement already satisfied: opencv-python-headless in /home/jovyan/anaconda3/envs/UniAD/lib/pytho

时间2023-04-11 11:07:32 提问来自 四川省成都市 的网友 分类创作 浏览290
...irement already satisfied: numpy>=1.14.5 in /home/jovyan/anaconda3/envs/UniAD/lib/python3.7/site-packages (from opencv-python-headless) (1.18.1)\n', 'Note: you may need to restart the kernel to use updated packages.\n'] > ["'4.2.0'"] § Code import cv2 # Import OpenCV Library import matplotlib as mpl # Import matplotlib library from matplotlib import pyplot as plt # Import pyplot library from matplotlib for plotting figures § Markdown ## Read and Display an Image using OpenCV and Matplotlib in Python To read an image using OpenCV in Python, use cv2.imread() function whose first argument takes the image path and second argument is a flag which specifies how to read an image, i.e., whether it should be read as a grayscale or color image such as cv2_IMREAD_COLOR, cv2_IMREAD_GRAYSCALE etc.. The return value of imread() is a NumPy array ndarray containing the values obtained from the specified file, converting it into a numerical array enables us to do mathematical operations on the values stored in images like addition, subtraction etc.. For displaying images we will use Matplotlib library which provides lots of functions for plotting graphs and figures of any shape or size; we will also use pyplot submodule of Matplotlib which provides many functions for plotting various plots like histogram, power spectra and so on.. To display an image with Matpoltib we will use imshow() function whose first argument takes the title of window and second argument takes the image data that can be either a NumPy array or PIL Image object; after executing this command our window containing our desired figure will appear on screen.. ### Read Image using OpenCV We are going to read lena.png file which is available inside data folder; if you don't have this file then you can download it from internet by searching lena png file.. After reading an image with OpenCV its data type will be converted into NumPy array so that we can perform mathematical operations on its values.. Now let's see how to read lena png file using OpenCV library in Python: § Code # Read lena png file from data folder with imread() function of opencv library and store its contents inside img variable # Specify flag value as 0 for reading it as gray scale img = cv2 .imread( 'data /lena .png ', 0 ) print (img) print ('Image Shape :', img .shape ) print ('Image Datatype :', img .dtype ) Output : [[161 162 163 ... 169 166 160] [162 162 162 ... 169 168 159] [163 161 160 ... 170 167 160] ... [ 45 44 45 ... 103 101 97] [ 42 41 40 ... 104 102 98] [ 38 37 36 ... 105 103 99]] Image Shape : (512 , 512) Image Datatype : uint8 As output shows us that our loaded image has 512 x 512 shape matrix i .e., 2D matrix containing pixel intensity values stored inside uint8 datatype variable .. Now let's display this loaded figure by using imshow() function of Matpoltib .. Display Loaded Figure Using Matpoltib Before displaying any figure make sure your environment supports GUI libraries otherwise you won't able to see anything .. Let's check whether our environment supports GUI libraries or not by importing pyQt5 module if no error appears then congratulations! Your environment supports GUI libraries other wise install Qt5 module before proceeding further .. import PyQt5 If no error appears then proceed further otherwise install Qt5 module by executing following command inside terminal .. pip install PyQt5 Now let's display our loaded figure by using imshow() function available inside pyPlot submodule of matpoltib library: plt .imshow(img ,cmap = 'gray' , interpolation = 'bicubic' ) plt .xticks([]) ,plt .yticks([]) #to hide tick values on X & Y axis plt .title('GrayScale Lena') plt .show () Output : As output shows us that now we are able to see gray scale Lena figure displayed over screen successfully; here I used two parameters inside imshow() method i.,e.,cmap & interpolation where cmap stands for color map used for mapping scalar data points into colors while interpolation helps us smoothing jagged edges appeared due some compression algorithms applied over original source images... Note: If your system doesn't support GUI libraries then comment out these lines before executing above code snippet otherwise program won't execute properly... import PyQt5 Color Map Used For Mapping Scalar Data Points Into Colors We can also change color map used while displaying figures simply pass different colormap name instead of gray e.,g., binary, hot etc… Let’s change colormap name passed into imshow method from ‘gray’ to ‘binary’ & execute above code snippet again… plt .imshow(img ,cmap = 'binary' , interpolation = 'bicubic' ) Output : Here black color represents high intensity while white represents low intensity… Interpolation Used For Smoothing Jagged Edges Interpolation parameter helps us smoothing jagged edges appeared due some compression algorithms applied over original source images… Here bicubic interpolation algorithm used for smoothing jagged edges but we can also replace bicubic with nearest neighbor algorithm simply pass nearest parameter instead bicubic e.,g.,plt …imshow(img ,cmap = ‘binary’ ,interpolation=’nearest’) Output : As output shows us now jagged edges disappeared because nearest neighbor algorithm replaced biqubic algorithm but keep in mind nearest neighbor algorithm causes pixelated effect over smooth surfaces… Summary In this tutorial we learned how easily one can load & display any kind of figure using opencv & matpoltib libraries respectively … Hope you enjoyed reading this tutorial if yes then don’ t forget sharing it with others who might find it helpful too … Happy Coding!!! :) # Read lena png file from data folder with IMREAD_GRAYSCALE flag value setted up as 0 img=cv2.imread('data/lena.png',0) print(img) print('Image Shape:',img.shape) print('Image Datatype:',img.dtype) Output: [[161 162 163 ... 169 166 160] [162 162 162 ... 169 168 159] [163 161 160 ... 170 167 160] ... [ 45 44 45 ... 103 101 97] [ 42 41 40... 104 102 98] [ 38 37 36... 105 103 99]] Image Shape:(512,512) Image Datatype:uint8 As output shown above that Our loaded image has 512x512 shape matrix i,.e 2D Matrix containing pixel intensity values stored inside uint8 datatype variable.....Now lets Display This Loaded Figure By Using IMShow Function Of MatPlotLib Library.... Display Loaded Figure Using MatPlotLib Before Displaying Any Figure Make Sure Your Environment Supports GUI Libraries Otherwise You Wont Able To See Anything....Lets Check Whether Our Environment Supports GUI Libraries Or Not By Importing PyQT Module If No Error Appears Then Congratulations!Your Environment Supports GUI Libraries Other Wise Install QT Module Before Proceeding Further..... import PyQT If No Error Appears Then Proceed Further OtherWise Install QT Module By Executing Following Command Inside Terminal.... pip install PyQT Now Lets Display Our Loaded Figure By Using IMShow Function Available Inside PyPlot SubModule Of MATPLOTLIB Library..... plt.imshow(img,cmap='gray',interpolation='biqubuc') pl

点击这里,进行内容创作

创作工场 免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!

上一篇 请结合大数据环境下个人信息及其保护的特点,谈谈个人信息保护政策的发展方向 下一篇 能否再详细一点,包括历史方面,谢谢!