pythonopencv圖片
① python OpenCV視頻拆分圖片代碼
# coding:utf-8
import cv2
import numpy as np
import os
print("1")
vc = cv2.VideoCapture("123.mp4")
C = 1
print("2")
if vc.isOpened():
rVal, frame = vc.read()
else:
print("3")
rVal = False
while rVal:
print(C)
if C % 1000 == 0: # every 5 fps write frame to img
path='./image/'+str(C)+'.jpg'
cv2.imwrite(path, frame)
# cropped001 = frame2[0:300,300:600] #y change from 0 to 300 x change from 300 to 600
# cv2.im write('./cropped/'+str(c)+'001.jpg',cropped001)
print(C)
cv2.waitKey(1)
C = C + 1
vc.release()
② opencv和python下,對圖片的讀取程序報錯如何解決
1、需要用到其他模塊的函數,如:
for i in range(20) #循環次數
image=cv2.imread("D:\picture\%d.jpg"%(i))#路徑自己選擇。
2、可以先升級你的pip,另外看看你的版本是否匹配,包括py版本和32位64位。
3、關於python下使用opencv讀取圖像。首先需要導入opencv包,上面說的那個Ipython並沒有opencv包,所以想使用的請先正確導入opencv包再說,至於怎麼導入,先下載個opencv包,裡面有關於python的opencv包。
以下照片是關於Ipython的運行界面:
③ 基於Python的opencv從csv文件中讀取圖片的問題
報錯異常說明
參數類型不正確
④ 為什麼用Python的openCV讀取圖片與PIL讀取的圖片像素值會不一樣
經測試,同一張圖片,使用 PIL和 OpenCv庫讀取的數據是一樣的(經過BGR轉成RGB):
建議:可以嘗試更新 PIL或是 OpenCv庫。
本機測試環境: Python 3.7+Pillow 6.2 +opencv-python 4.1
⑤ python opencv 顯示圖片 未響應
整個項目的結構圖:
編寫DetectFaceDemo.java,代碼如下:
[java] view
plainprint?
package com.njupt.zhb.test;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;
//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
public class DetectFaceDemo {
public void run() {
System.out.println("\nRunning DetectFaceDemo");
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());
// Create a face detector from the cascade file in the resources
// directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("森段並lbpcascade_frontalface.xml").getPath());
//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());
//注意:源程序的路徑會多列印一個燃豎『/』,因此總是出現如下錯誤
/*
* Detected 0 faces Writing faceDetection.png libpng warning: Image
* width is zero in IHDR libpng warning: Image height is zero in IHDR
* libpng error: Invalid IHDR data
*/
//因此,我們將第一個字元去掉
String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("此跡Detected %s faces", faceDetections.toArray().length));
// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
// Save the visualized detection.
String filename = "faceDetection.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);
}
}
package com.njupt.zhb.test;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;
//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
public class DetectFaceDemo {
public void run() {
System.out.println("\nRunning DetectFaceDemo");
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());
// Create a face detector from the cascade file in the resources
// directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());
//注意:源程序的路徑會多列印一個『/』,因此總是出現如下錯誤
/*
* Detected 0 faces Writing faceDetection.png libpng warning: Image
* width is zero in IHDR libpng warning: Image height is zero in IHDR
* libpng error: Invalid IHDR data
*/
//因此,我們將第一個字元去掉
String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
// Save the visualized detection.
String filename = "faceDetection.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);
}
}
3.編寫測試類:
[java] view
plainprint?
package com.njupt.zhb.test;
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello, OpenCV");
// Load the native library.
System.loadLibrary("opencv_java246");
new DetectFaceDemo().run();
}
}
//運行結果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png
package com.njupt.zhb.test;
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello, OpenCV");
// Load the native library.
System.loadLibrary("opencv_java246");
new DetectFaceDemo().run();
}
}
//運行結果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png
⑥ 基於python語言的opencv如何把圖片中指定區域截取出來
3-切割輪廓
(這是我網站找的一篇 blog, 親測有效)
⑦ python3中如何載入圖片
答: 如下所示。
- 可利用opencv-Python介面,使用imread()函數,那麼導入名為example的圖片的例子如下所示。
import cv2
image = cv2.imread('./example.png')
- 也可以使用matplotlib.pyplot中的pyplot模塊,具體例子如下所示。
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
image = mpimg.imread('./example.png')
print image.shape
plt.imshow(image) #調用imshow函數
在這里只是說了兩種方法,希望能夠幫助到你。
⑧ python-opencv怎麼在不影響圖片中其他顏色的情況下,將黑色背景變成白色背景
設前景色為白色,選擇--色彩范圍,點選黑色,作反相處理,調整色階至滿意,局部細節用畫筆修整一下即可。
⑨ opencv和python下,運行對圖片的讀取程序時出現的問題!!!
1、需要姿頃用到其他模塊的函數,如:
for i in range(20) #循環次數
image=cv2.imread("D:\picture\%d.jpg"%(i))#路徑自跡掘陸己選擇。
2、可以先升級你的pip,另外看看你的版本是否匹配,包括py版本和32位64位。
3、關於python下使用opencv讀取圖像。首先散毀需要導入opencv包,上面說的那個Ipython並沒有opencv包,所以想使用的請先正確導入opencv包再說,至於怎麼導入,先下載個opencv包,裡面有關於python的opencv包。
以下照片是關於Ipython的運行界面:
⑩ python opencv如何存圖片到指定路徑按圖上的會存到python_work文件夾
如圖,修改倒數第四行的內容為:
cv2.imwrite('F:/xxx/yyy/' + str(c) + '.jpg', frame)
即可將圖片存儲到 F 盤的 xxx\yyy 目錄中,這里按照你的需要修改即可