[英]Reading Image Stream from RCCC Bayer Camera Sensor in Ubuntu
我正在使用LI-AR0820 GMSL2相機,該相機使用On-Semi AR0820傳感器以12位RCCC Bayer格式捕獲圖像。 我想從相機讀取實時圖像流,並將其轉換為灰度圖像(使用此去馬賽克算法),然后將其輸入到對象檢測算法中。 但是,由於OpenCV不支持RCCC格式,因此無法使用VideoCapture類從攝像機獲取圖像數據。 我正在尋找類似的東西來以類似數組的格式獲取流式圖像數據,以便我可以對其進行進一步的操作。 有任何想法嗎?
我正在運行帶有OpenCV 3.2.0和Python 3.7.1的Ubuntu 18.04。
編輯。 我在這里使用代碼。
#include <vector>
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
int main() {
// Each pixel is made up of 16 bits, with the high 4 bits always equal to 0
unsigned char bytes[2];
// Hold the data in a vector
std::vector<unsigned short int> data;
// Read the camera data
FILE *fp = fopen("test.raw","rb");
while(fread(bytes, 2, 1, fp) != 0) {
// The data comes in little-endian, so shift the second byte right and concatenate the first byte
data.push_back(bytes[0] | (bytes[1] << 8));
}
// Make a matrix 1280x720 with 16 bits of unsigned integers
cv::Mat imBayer = cv::Mat(720, 1280, CV_16U);
// Make a matrix to hold RGB data
cv::Mat imRGB;
// Copy the data in the vector into a nice matrix
memmove(imBayer.data, data.data(), data.size()*2);
// Convert the GR Bayer pattern into RGB, putting it into the RGB matrix!
cv::cvtColor(imBayer, imRGB, CV_BayerGR2RGB);
cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);
// *15 because the image is dark
cv::imshow("Display window", 15*imRGB);
cv::waitKey(0);
return 0;
}
代碼有兩個問題。 首先,我必須使用fswebcam獲取原始圖像文件,然后使用上面的代碼讀取原始文件並顯示圖像。 我希望能夠訪問/ dev / video1節點並直接從那里讀取原始數據,而不必先保存然后再單獨讀取。 其次,OpenCV不支持RCCC Bayer格式,因此我必須提出一種去馬賽克方法。
相機通過同軸電纜輸出串行數據,因此我使用USB 3.0連接的Deser板將相機連接到筆記本電腦。 設置可以在這里看到。
如果您的攝像機支持CAP_PROP_CONVERT_RGB
屬性,則可以從VideoCapture獲取原始RCCC數據。 通過將此屬性設置為False
,可以禁用到RGB的轉換。 因此,您可以使用以下代碼捕獲原始幀(為簡單起見,不進行錯誤檢查):
cap = cv2.VideoCapture(0)
# disable converting images to RGB
cap.set(cv2.CAP_PROP_CONVERT_RGB, False)
while(True):
ret, frame = cap.read()
# other processing ...
cap.release()
我不知道這是否適用於您的相機。
如果可以某種方式獲得原始圖像,則可以應用“ ANALOG DEVICES”應用筆記中所述的去馬賽克方法。
帶有最佳過濾器
我按照應用筆記中的說明編寫了以下python代碼,以測試RCCC->灰色轉換。
import cv2
import numpy as np
rgb = cv2.cvtColor(cv2.imread('RGB.png'), cv2.COLOR_BGR2RGB)
c = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY)
r = rgb[:, :, 0]
# no error checking. c shape must be a multiple of 2
rmask = np.tile([[1, 0], [0, 0]], [c.shape[0]//2, c.shape[1]//2])
cmask = np.tile([[0, 1], [1, 1]], [c.shape[0]//2, c.shape[1]//2])
# create RCCC image by replacing 1 pixel out of 2x2 pixel region
# in the monochrome image (c) with a red pixel
rccc = (rmask*r + cmask*c).astype(np.uint8)
# RCCC -> GRAY conversion
def rccc_demosaic(rccc, rmask, cmask, filt):
# RCCC -> GRAY
# use border type REFLECT_101 to give correct results for border pixels
filtered = cv2.filter2D(src=rccc, ddepth=-1, kernel=filt,
anchor=(-1, -1), borderType=cv2.BORDER_REFLECT_101)
demos = (rmask*filtered + cmask*rccc).astype(np.uint8)
return demos
# demo of the optimal filter
zeta = 0.5
kernel_4neighbor = np.array([[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0]])/4.0
kernel_optimal = np.array([[0, 0, -1, 0, 0],
[0, 0, 2, 0, 0],
[-1, 2, 4, 2, -1],
[0, 0, 2, 0, 0],
[0, 0, -1, 0, 0]])/8.0
kernel_param = np.array([[0, 0, -1./4, 0, 0],
[0, 0, 0, 0, 0],
[-1./4, 0, 1., 0, -1./4],
[0, 0, 0, 0, 0],
[0, 0, -1./4, 0, 0]])
# apply optimal filter (Figure 7)
opt1 = rccc_demosaic(rccc, rmask, cmask, kernel_optimal)
# parametric filter with zeta = 0.5 (Figure 5)
opt2 = rccc_demosaic(rccc, rmask, cmask, kernel_4neighbor + zeta * kernel_param)
# PSNR
print(10 * np.log10(255**2/((c - opt1)**2).mean()))
print(10 * np.log10(255**2/((c - opt2)**2).mean()))
輸入RGB 圖像 :
模擬的RCCC圖像:
去馬賽克算法的灰度圖像:
還有一件事:
如果您的相機供應商提供了Linux的SDK,則它可能具有執行RCCC-> GREY轉換的API,或至少獲取了原始圖像。 如果SDK中沒有RCCC-> GREY轉換,則C#示例代碼應該包含它,因此我建議您看一下它們的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.