我正在使用OpenCV开展视频捕捉项目。我已经能够使视频捕获工作,但我想将相机滤镜更改为“常规”光谱,以便香蕉不是蓝色,是黄色的哈哈。这将是RGB光谱。我在for循环中的程序中进行了转换,但我的相机视图仍然是BRG。这是一张图片。
如何让图像视图显示“真实”颜色?这是我的代码。
Mat to Image Class
import java.awt.Color;
import java.awt.image.BufferedImage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
public class Mat2Image {
Mat mat = new Mat();
private int argb = 0;
Color c = new Color(argb, true);
BufferedImage img;
byte [] dat;
Mat2Image(){
}
public Mat2Image(Mat mat) {
getSpace(mat);
}
public void getSpace(Mat mat) {
this.mat = mat;
int w = mat.cols(), h = mat.rows();
if (dat == null || dat.length != w * h * 3)
dat = new byte[w * h * 3];
if (img == null || img.getWidth() != w || img.getHeight() != h || img.getType() != BufferedImage.TYPE_3BYTE_BGR)
img = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
int[] holder = new int[w * h];
holder = img.getData().getPixels(0, 0, w, h, (int[]) null);
for (int i = 0; i < dat.length; i++) {
int blueChannel = (holder[i] & 0xFF0000) >> 4;
int greenChannel = (holder[i] & 0xFF00) >> 2;
int redChannel = (holder[i] & 0xFF);
int rgb = (blueChannel) & (greenChannel << 2) & (redChannel << 4);
dat[i] = (byte) rgb;
}
}
BufferedImage getImage(Mat mat) {
getSpace(mat);
mat.get(0, 0, dat);
img.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), dat);
return img;
}
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
}
视频捕获类
public class VideoCap {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
int currFrame = 0;
VideoCapture cap;
Mat2Image mat2Img = new Mat2Image();
VideoCap() {
cap = new VideoCapture();
cap.open(0);
}
BufferedImage getOneFrame() {
currFrame++;
//currFrame is just to stop the camera of after the running for a bit.
if (currFrame == 20) {
cap.read(mat2Img.mat);
System.exit(0);
}
cap.read(mat2Img.mat);
return mat2Img.getImage(mat2Img.mat);
}
}
我的JFrame类
public class MyFrame extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyFrame frame = new MyFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MyFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,650,490);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
new MyThread().start();
}
VideoCap videoCap = new VideoCap();
public void paint(Graphics g){
g = contentPane.getGraphics();
g.drawImage(videoCap.getOneFrame(), 0, 0, this);
}
class MyThread extends Thread{
@Override
public void run() {
for (;;){
repaint();
try { Thread.sleep(30);
} catch (InterruptedException e) { }
}
}
}
}
答案 0 :(得分:1)
你正在重新排列错误的颜色 - 你没有移动足够的位,你应该使用OR(<p>
What is your name?
<input id="name-input" type="text" />
<button id="submit-name" type="button">Submit it</button>
</p>
<p class="greeting" id="howdy">
Howdy, <span id="name-output">student</span>!
</p>
<h2>Brainstorms</h2>
<p class="info" id="webappidea"> Text</p>
)而不是AND(|
)运算符来重新组合颜色。
您的代码:
&
应该成为:
int blueChannel = (holder[i] & 0xFF0000) >> 4;
int greenChannel = (holder[i] & 0xFF00) >> 2;
int redChannel = (holder[i] & 0xFF);
int rgb = (blueChannel) & (greenChannel << 2) & (redChannel << 4);
修改的
此外,您似乎没有使用int blueChannel = (holder[i] & 0xFF0000) >> 16;
int greenChannel = (holder[i] & 0xFF00) >> 8;
int redChannel = (holder[i] & 0xFF);
int rgb = (blueChannel) | (greenChannel << 8) | (redChannel << 16);
作为目标图片,而是使用3字节的颜色编码。因此,您更有可能需要这样做:
int[]
但是我不清楚你是从BGR转向RGB还是从RGB转向BGR。您可能需要更改最后3个分配的顺序才能使其正确。
答案 1 :(得分:1)
我昨天花了很多时间试图打破它,我终于开始工作了。我做的第一件事是分配新图像。这是在getSpace
中完成的。然后我转换分配的图像,然后从矩阵中捕获数据,然后将数据放入图像。最后3个步骤都在getImage
进行。这是我如何解决它。
public void getSpace(Mat mat) {
this.mat = mat;
int w = mat.cols(), h = mat.rows();
if (dat == null || dat.length != w * h * 3)
dat = new byte[w * h * 3];
if (img == null || img.getWidth() != w || img.getHeight() != h || img.getType() != BufferedImage.TYPE_3BYTE_BGR)
img = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
}
BufferedImage getImage(Mat mat) {
getSpace(mat);
mat.get(0, 0, dat);
for (int i = 0; i < dat.length; i+= 3) {
byte redChannel = dat[i];
byte blueChannel = dat[i+2];
dat[i] = blueChannel;
dat[i+2] = redChannel;
}
img.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), dat);
return img;
}
Mat2Image类中的其余代码应与我在答案中未添加的代码相同,您可以尝试一下。 :)