gpt4 book ai didi

java - 将颜色 channel 从 BGR 转换为 RGB

转载 作者:太空宇宙 更新时间:2023-11-03 22:25:23 25 4
gpt4 key购买 nike

我正在使用 OpenCV 进行视频捕获项目。我已经能够让视频捕捉正常工作,但我想将相机滤镜更改为“常规”光谱,这样香蕉就不是蓝色而是黄色了哈哈。这将是 RGB 光谱。我在我的程序中的 for 循环中进行了转换,但我的相机 View 仍然是 BRG。这是一张图片。 enter image description here

如何让 ImageView 显示“真实”颜色?这是我的代码。

Mat 到图像类

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) { }
}
}
}
}

最佳答案

您错误地重新排列了颜色 - 您没有移动足够多的位,您应该使用 OR (|) 而不是 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);

编辑

此外,您似乎没有对目标图像使用 int[],而是对颜色使用 3 字节编码。所以你更有可能需要做:

for (int i = 0; i < holder.length; i++) {
byte blueChannel = (byte) ((holder[i] & 0xFF0000) >> 16);
byte greenChannel = (byte) ((holder[i] & 0xFF00) >> 8);
byte redChannel = (byte) (holder[i] & 0xFF);

dat[i*3] = redChannel;
dat[i*3+1] = greenChannel;
dat[i*3+2] = blueChannel;

但我不清楚是从 BGR 转到 RGB 还是从 RGB 反向转到 BGR。您可能需要更改最后 3 项分配的顺序才能正确执行此操作。

关于java - 将颜色 channel 从 BGR 转换为 RGB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41820240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com