white2black 从的到黑找边沿点 public static Point FindExtremePoint(Mat image,boo white2black) { // 搜索方向为黑色到白色,最大值为255 double minVal; double maxVal; Point minLoc; Point maxLoc; Cv2.MinMaxLoc(image, out minVal, out maxVal, out minLoc, out maxLoc, image); // 返回极值点坐标 return maxLoc; },请优化代码
时间: 2024-03-09 18:49:02 浏览: 71
这段代码可以进行一些优化:
1. 首先,可以将参数类型从Mat改为Mat<float>,这样可以提高代码的效率。
2. 可以将搜索方向和最大值设为常量,这样可以避免重复计算和提高代码的可读性。
3. 可以添加异常处理,以避免在图像为空时出现异常。
下面是优化后的代码:
```
public static Point FindExtremePoint(Mat<float> image, bool white2black)
{
const double maxVal = 255.0;
const MinMaxLocResult searchDirection = white2black ? MinMaxLocRresult.Min : MinMaxLocResult.Max;
if (image == null)
{
throw new ArgumentNullException(nameof(image));
}
Point minLoc, maxLoc;
double minVal, maxVal;
Cv2.MinMaxLoc(image, out minVal, out maxVal, out minLoc, out maxLoc, image);
return searchDirection == MinMaxLocResult.Min ? minLoc : maxLoc;
}
```
这个版本的代码在搜索边缘点时,只搜索了一次,同时代码也更加易读和健壮。
相关问题
public class Machine { boolean isDanger; String name; public void setIsDanger(boolean boo) { isDanger = boo; } public boolean checkBag () //【代码0】声明异常 { DangerException danger=new DangerException() ; 【代码1】 //抛出danger }
在【代码0】处,可以声明一个 `throws DangerException` 来表示该方法可能会抛出一个 `DangerException` 异常。在【代码1】处,可以使用 `throw danger` 来抛出该异常。完整代码如下:
```
public class Machine {
boolean isDanger;
String name;
public void setIsDanger(boolean boo) {
isDanger = boo;
}
public boolean checkBag() throws DangerException {
DangerException danger = new DangerException();
throw danger;
}
}
```
请按模板要求,将【代码】替换为Java程序代码。Goods.javapublic class Goods { boolean isDanger; private String name; public void setIsDanger(boolean boo) { isDanger = boo; } public boolean isDanger() { return isDanger; } public void setName(String s) { name = s; } public String getName() { return name; }}DangerException.javapublic class DangerException extends Exception { String message; public DangerException() { message = "危险品!"; } public void toShow() { System.out.print(message+" "); }}Machine.javapublic class Machine { boolean isDanger; String name; public void setIsDanger(boolean boo) { isDanger = boo; } public boolean checkBag () //【代码0】声明异常 { DangerException danger=new DangerException() ; 【代码1】 //抛出danger }Check.javapublic class Check { public static void main(String args[]) { Machine machine = new Machine(); String name[] ={"苹果","炸药","西服","硫酸","手表","硫磺"}; Goods [] goods = new Goods[name.length];
// Goods.java
public class Goods {
private boolean isDanger;
private String name;
public void setIsDanger(boolean boo) {
isDanger = boo;
}
public boolean isDanger() {
return isDanger;
}
public void setName(String s) {
name = s;
}
public String getName() {
return name;
}
}
// DangerException.java
public class DangerException extends Exception {
private String message;
public DangerException() {
message = "危险品!";
}
public void toShow() {
System.out.print(message + " ");
}
}
// Machine.java
public class Machine {
private boolean isDanger;
private String name;
public void setIsDanger(boolean boo) {
isDanger = boo;
}
public boolean checkBag() throws DangerException { // 【代码0】声明异常
if(isDanger) {
throw new DangerException();
}
else {
return false;
}
}
}
// Check.java
public class Check {
public static void main(String args[]) {
Machine machine = new Machine();
String name[] = {"苹果", "炸药", "西服", "硫酸", "手表", "硫磺"};
Goods[] goods = new Goods[name.length]; // 检查6件物品
for(int i = 0; i < name.length; i++) {
goods[i] = new Goods();
goods[i].setName(name[i]);
if(i == 1 || i == 3 || i == 5) {
goods[i].setIsDanger(true);
machine.setIsDanger(true);
}
else {
goods[i].setIsDanger(false);
}
try {
boolean isDangerous = machine.checkBag();
if(isDangerous) {
System.out.println(goods[i].getName() + "是危险品!");
}
else {
System.out.println(goods[i].getName() + "是安全品!");
}
}
catch(DangerException e) { // 【代码1】抛出DangerException异常
e.toShow();
System.out.println(goods[i].getName() + "是危险品!");
}
}
}
}
阅读全文