我目前正在使用Context API结合useReducer
钩子在React中实现全局状态处理。
我对可变性有两个担忧:
cloneDeep
函数来切断进入我的化简器的对象与存储状态之间的引用? 以供参考:docs
答案 0 :(得分:0)
useState
和useReducer
都为您提供了保存的确切值引用(通过调用someSetter(newValue)
或从reducer函数返回值)。
在任何一种情况下,手动更改值都是错误的。特别是,如果您返回与上次相同的引用,它们两个都将无法更新,因此您应始终不变地更新值。
答案 1 :(得分:-1)
我会读一读。
https://kentcdodds.com/blog/how-to-use-react-context-effectively
这是如何正确地将React上下文与public class OrbDetector extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String bookObject = "/Users/yoelkastro/Desktop/bookobject.jpg";
String bookScene = "/Users/yoelkastro/Desktop/bookscene.jpg";
// Get images
Mat objectImage = Imgcodecs.imread(bookObject, Imgcodecs.IMREAD_COLOR);
Mat sceneImage = Imgcodecs.imread(bookScene, Imgcodecs.IMREAD_COLOR);
// Find keypoints and descriptor using ORB
ORB orb = ORB.create(500);
MatOfKeyPoint keypoints = new MatOfKeyPoint();
Mat descriptors = new Mat();
orb.detectAndCompute(objectImage, new Mat(), keypoints, descriptors);
Mat outputImage = new Mat(objectImage.rows(), objectImage.cols(), Imgcodecs.IMREAD_COLOR);
Features2d.drawKeypoints(objectImage, keypoints, outputImage);
// Find keypoints and descriptor for scene using ORB
ORB orb2 = ORB.create(500);
MatOfKeyPoint sceneKeypoints = new MatOfKeyPoint();
Mat sceneDescriptors = new Mat();
orb2.detectAndCompute(sceneImage, new Mat(), sceneKeypoints, sceneDescriptors);
Mat sceneOutputImage = new Mat(sceneImage.rows(), sceneImage.cols(), Imgcodecs.IMREAD_COLOR);
Features2d.drawKeypoints(sceneImage, sceneKeypoints, sceneOutputImage);
// Convert descriptors
descriptors.convertTo(descriptors, CvType.CV_32F);
sceneDescriptors.convertTo(sceneDescriptors, CvType.CV_32F);
DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
Mat matchOutput = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2, Imgcodecs.IMREAD_COLOR);
List<MatOfDMatch> matches = new LinkedList<MatOfDMatch>();
// Match the descriptors for the scene and object
descriptorMatcher.knnMatch(descriptors, sceneDescriptors, matches, 2);
LinkedList<DMatch> goodMatchesList = new LinkedList<DMatch>();
float nndrRatio = 0.8f;
// Find the number of good matches+
for (int i = 0; i < matches.size(); i++) {
MatOfDMatch matofDMatch = matches.get(i);
DMatch[] dmatcharray = matofDMatch.toArray();
DMatch m1 = dmatcharray[0];
DMatch m2 = dmatcharray[1];
if (m1.distance <= m2.distance * nndrRatio) {
goodMatchesList.addLast(m1);
}
}
System.out.println(goodMatchesList.size());
if (goodMatchesList.size() >= 4) {
System.out.println("Object Found!!!");
List<KeyPoint> objKeypointlist = keypoints.toList();
List<KeyPoint> scnKeypointlist = sceneKeypoints.toList();
LinkedList<Point> objectPoints = new LinkedList<>();
LinkedList<Point> scenePoints = new LinkedList<>();
for (int i = 0; i < goodMatchesList.size(); i++) {
objectPoints.addLast(objKeypointlist.get(goodMatchesList.get(i).queryIdx).pt);
scenePoints.addLast(scnKeypointlist.get(goodMatchesList.get(i).trainIdx).pt);
}
MatOfPoint2f objMatOfPoint2f = new MatOfPoint2f();
objMatOfPoint2f.fromList(objectPoints);
MatOfPoint2f scnMatOfPoint2f = new MatOfPoint2f();
scnMatOfPoint2f.fromList(scenePoints);
Mat homography = Calib3d.findHomography(objMatOfPoint2f, scnMatOfPoint2f, Calib3d.RANSAC, 3);
Mat obj_corners = new Mat(4, 1, CvType.CV_32FC2);
Mat scene_corners = new Mat(4, 1, CvType.CV_32FC2);
obj_corners.put(0, 0, new double[]{0, 0});
obj_corners.put(1, 0, new double[]{objectImage.cols(), 0});
obj_corners.put(2, 0, new double[]{objectImage.cols(), objectImage.rows()});
obj_corners.put(3, 0, new double[]{0, objectImage.rows()});
System.out.println("Transforming object corners to scene corners...");
Core.perspectiveTransform(obj_corners, scene_corners, homography);
Imgproc.line(sceneImage, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)), new Scalar(0, 255, 0), 4);
Imgproc.line(sceneImage, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)), new Scalar(0, 255, 0), 4);
Imgproc.line(sceneImage, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)), new Scalar(0, 255, 0), 4);
Imgproc.line(sceneImage, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)), new Scalar(0, 255, 0), 4);
System.out.println("Drawing matches image...");
MatOfDMatch goodMatches = new MatOfDMatch();
goodMatches.fromList(goodMatchesList);
Features2d.drawMatches(objectImage, keypoints, sceneImage, sceneKeypoints, goodMatches, matchOutput);
} else {
System.out.println("Object Not Found");
}
ImageView i = new ImageView(Test.mat2Img(matchOutput));
Group root = new Group();
root.getChildren().add(i);
stage.setScene(new Scene(root));
stage.show();
}
}
结合使用的很好指南。
让我知道是否有帮助。