我是使用Opencv和Java的新手。我有Mat
图像,我正在尝试读取特定区域中的像素,然后我可以循环遍历该区域并确定我尝试使用CvRect
来获取坐标的HSV和我想要的图像的区域的大小。我如何获得图像的那个区域?
Mat firstImage = Imgcodecs.imread("firstImage.png");
CvRect topL = new CvRect(firstImage.get(160, 120, 30, 30));
答案 0 :(得分:1)
有两种方法可以做到这一点:逐个读取像素,或者将整个图像中的矩形中的所有像素都放到java中,然后使用较大的数组。哪一个最好可能取决于矩形的大小。下面的代码首先将图像的指定rect部分放入Java数组中。
import java.awt.Color;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.highgui.Highgui;
public class OpenCVThing
{
public static void main(String[] args)
{
String opencvpath = System.getProperty("user.dir") + "\\lib\\";
System.load(opencvpath + Core.NATIVE_LIBRARY_NAME + ".dll");
// Get the whole rect into smallImg
Mat firstImage = Highgui.imread("capture.png");
System.out.println("total pxels:" + firstImage.total());
// We are getting a column 30 high and 30 wide
int width = 30;
int height = 30;
Rect roi = new Rect(120, 160, width, height);
Mat smallImg = new Mat(firstImage, roi);
int channels = smallImg.channels();
System.out.println("small pixels:" + smallImg.total());
System.out.println("channels:" + smallImg.channels());
int totalBytes = (int)(smallImg.total() * smallImg.channels());
byte buff[] = new byte[totalBytes];
smallImg.get(0, 0, buff);
// assuming it's of CV_8UC3 == BGR, 3 byte/pixel
// Effectively assuming channels = 3
for (int i=0; i< height; i++)
{
// stride is the number of bytes in a row of smallImg
int stride = channels * width;
for (int j=0; j<stride; j+=channels)
{
int b = buff[(i * stride) + j];
int g = buff[(i * stride) + j + 1];
int r = buff[(i * stride) + j + 2];
float[] hsv = new float[3];
Color.RGBtoHSB(r,g,b,hsv);
// Do something with the hsv.
System.out.println("hsv: " + hsv[0]);
}
}
}
}
注1 :在这种情况下,buff中的每个字节代表一个像素的三分之一,因为我假设格式为CV_8UC3。
使用以下输出在此答案的屏幕截图上测试代码:
total pxels:179305
small pixels:900
channels:3
hsv: 0.5833333
hsv: 0.5833333
hsv: 0.5833333
hsv: 0.5833333
hsv: 0.5833333
etc ...