在JavaCV中将Scalar转换为Mat

时间:2015-03-11 14:54:55

标签: java opencv javacv

使用JavaCV我尝试将不同的颜色应用于输入范围内两个数字之间的区域。请参阅以下代码,了解它应如何工作的示例:

Mat mask = new Mat();
inRange(intensity, new Scalar(0.0), new Scalar(0.3), mask);
image.setTo(new Scalar(255.0, 0.0, 0.0, 1.0), mask);

我无法编译该代码,因为JavaCV似乎期望inRange和setTo中都有Mat而不是Scalar对象,请参阅此处的定义:

@Namespace("cv") public static native void inRange(@ByVal Mat src,
       @ByVal Mat lowerb, @ByVal Mat upperb, @ByVal Mat dst);

在OpenCV文档Scalar中应该没有问题,而不是Mat。有没有办法将Scalar转换为Mat,这样可行?

我注意到我可以使用cvInRangeS代替inRange,但我无法找到与setTo功能等效的内容。

1 个答案:

答案 0 :(得分:0)

在使用格式inRange Mat OpenCV Error: Sizes of input arguments do not match (The lower bounary is neither an array of the same size and same type as src, nor a scalar)进行调用时,您应该收到的错误是Mat,或者其他内容(请注意拼写错误)。

如果您想将Scalar用作Mat,则可以使用double[]Scalar对象初始化new Mat(new double[]{255, 255, 255, 0})。例如:private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { //frame e.Graphics.DrawRectangle(Pens.Black, 30, 30, 757, 1080); //rows e.Graphics.DrawLine(Pens.Black, 30, 184, 787, 184); e.Graphics.DrawLine(Pens.Black, 30, 338, 787, 338); e.Graphics.DrawLine(Pens.Black, 30, 492, 787, 492); e.Graphics.DrawLine(Pens.Black, 30, 646, 787, 646); e.Graphics.DrawLine(Pens.Black, 30, 800, 787, 800); e.Graphics.DrawLine(Pens.Black, 30, 955, 787, 955); //columns e.Graphics.DrawLine(Pens.Black, 282, 30, 282, 1110); e.Graphics.DrawLine(Pens.Black, 534, 30, 534, 1110); int rows_loc = 35; int cols_loc = 70; int lable_index = 0; foreach (ListViewItem item in listView1.Items) { for (int quantity = 1; quantity < (Convert.ToInt32(item.SubItems[1].Text) + 1); quantity++) { Font font = new System.Drawing.Font("Arial", 16); SolidBrush black = new SolidBrush(Color.Black); Zen.Barcode.Code128BarcodeDraw barcode1 = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum; Image barcode_image = barcode1.Draw(item.SubItems[2].Text, 20); e.Graphics.DrawImage(barcode_image, 40f + cols_loc, 29f + rows_loc); e.Graphics.DrawString(item.Text, font, black, 60f + cols_loc, 0f + rows_loc); lable_index++; row_index = (int)(lable_index / 3); col_index = lable_index % 3; rows_loc = 35 + ((row_index) * 155); cols_loc = 70 + (250 * col_index); if (rows_loc >= e.PageSettings.PrintableArea.Height) { e.HasMorePages = true; rows_loc = 35; return; } else { e.HasMorePages = false; } } } }