不显示二值图像
我正在进行图像检测,必须对每个红色、绿色和蓝色元素进行处理以获得边缘图(黑白形式的二进制图像)并将它们组合成一个以显示输出。当我提取每个红色、绿色和蓝色的元素并设置阈值以获得二值图像后,它不显示二值图像。相反,它向我显示灰度图像。有人愿意帮助我吗?到目前为止,这是我的代码。
Buffered Image buff_red;
int[] process_red;
int width = 256;
int height = 256;
private void processActionPerformed(java.awt.event.ActionEvent evt) {
width = inputimage.getWidth(null);
height = inputimage.getHeight(null);
buff_red = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics r = buff_red.getGraphics();
r.drawImage(inputimage, 0, 0, null);
r.dispose();
//get the red element
process_red = new int[width * height];
counter = 0;
for(int i = 0; i < 256; i++) {
for(int j = 0; j < 256; j++) {
int clr = buff_red.getRGB(j, i);
int red = (clr & 0x00ff0000) >> 16;
red = (0xFF<<24)|(red<<16)|(red<<8)|red;
process_red[counter] = red;
counter++;
}
}
//set threshold value for red element
int threshold = 100;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int bin = (buff_red.getRGB(x, y) & 0x000000ff);
if (bin < threshold)
bin = 0;
else
bin = 255;
buff_red.setRGB(x,y, 0xff000000 | bin << 16 | bin << 8 | bin);
}
}
更新:
buff_red
的初始化是在“获取红色元素”(第一个循环)之前完成的,即:
buff_red = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics r = buff_red.getGraphics();
r.drawImage(inputimage, 0, 0, null);
我应该缓冲来自 process_red
的图像并将其用于阈值方法吗?我可以获得边缘图吗?
I'm doing an image detection which I have to process on each red, green, and blue element to get the edge map (binary image in black and white form) and combine them become one to show the output. After I extract the element of each red, green, and blue and set thresholding value to get the binary image, it doesn't show the binary image. Instead it shows me the grayscale image. Would anyone please be kind enough to help me? Here is my code so far.
Buffered Image buff_red;
int[] process_red;
int width = 256;
int height = 256;
private void processActionPerformed(java.awt.event.ActionEvent evt) {
width = inputimage.getWidth(null);
height = inputimage.getHeight(null);
buff_red = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics r = buff_red.getGraphics();
r.drawImage(inputimage, 0, 0, null);
r.dispose();
//get the red element
process_red = new int[width * height];
counter = 0;
for(int i = 0; i < 256; i++) {
for(int j = 0; j < 256; j++) {
int clr = buff_red.getRGB(j, i);
int red = (clr & 0x00ff0000) >> 16;
red = (0xFF<<24)|(red<<16)|(red<<8)|red;
process_red[counter] = red;
counter++;
}
}
//set threshold value for red element
int threshold = 100;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int bin = (buff_red.getRGB(x, y) & 0x000000ff);
if (bin < threshold)
bin = 0;
else
bin = 255;
buff_red.setRGB(x,y, 0xff000000 | bin << 16 | bin << 8 | bin);
}
}
Update:
Initialization of buff_red
was done before the "get red element" (first loop), which is:
buff_red = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics r = buff_red.getGraphics();
r.drawImage(inputimage, 0, 0, null);
Should I buffer the image from process_red
and use it for thresholding method so that I can get the edge map?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的代码中看起来可疑的一件事是您的“获取红色元素步骤”(第一个循环)写入
process_red
,但您的阈值步骤(第二个循环)从buf_red
读取,它似乎没有在任何地方初始化。这是一个拼写错误,还是您的代码中的错误?您提到了边缘检测,但我在您发布的代码中看不到任何看起来像边缘检测的内容。您似乎所做的就是提取红色(绿色,蓝色)通道,对它们进行阈值处理,然后将它们组合起来。
如果您在解决问题时更具分析性,将会有所帮助。问题最早出现在什么时候?您是否正确地从图像中提取通道?您的边缘检测图像看起来正确吗?您的阈值结果是否符合您的预期?您可以自己回答所有这些问题——编写/显示调试图像。
最后,理想情况下,您不必通过自己编码来手动编写此类低级普通任务(获取像素、通过
0xff
进行掩码等),至少在 Java 中是这样。第一次很有趣,但之后它就只是错误和意外功能的另一个来源。我目前不使用 Java,但我确信它有一个图像处理 API 可以为您处理此类任务。One thing that looks suspicious from your code is that your "get red element step" (first loop) writes to
process_red
, but your thresholding step (second loop) reads frombuf_red
, which doesn't seem to be initialized anywhere. Is this a typo, or a bug in your code?You mention edge detection, but I can't see anything that looks like edge detection in the code that you posted. All you seem to be doing is extracting the red (green, blue) channels, thresholding them, and then combining them.
It would help if you were more analytical in your approach to the problem. What is the earliest point where the problem manifests yourself? Are you extracting the channels from the image correctly? Do your edge detection images look right? Is your thresholding result giving you what you expect? You can answer all of these questions by yourself -- write/show debug images.
Finally, ideally you shouldn't have to manually code such low-level mundane tasks (fetch pixel, mask by
0xff
, etc) by coding them yourself, at least in Java. It's fun the first time through, but after that it's just another source for bugs and unexpected features. I don't currently use Java, but I'm certain that it has an image processing API that can handle such tasks for you.