小编Aid*_*hjj的帖子

OpenCV - 正确使用cv2.approxPolyDP()

我试图使用提取图像中轮廓的近似值cv2.approxPolyDP().这是我正在使用的图像:

英国地图

我的代码试图隔离主岛并定义和绘制轮廓近似和轮廓外壳.我绘制了以绿色找到的轮廓,近似为红色:

import numpy as np
import cv2

# load image and shrink - it's massive
img = cv2.imread('../data/UK.png')
img = cv2.resize(img, None,fx=0.25, fy=0.25, interpolation = cv2.INTER_CUBIC)

# get a blank canvas for drawing contour on and convert img to grayscale
canvas = np.zeros(img.shape, np.uint8)
img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# filter out small lines between counties
kernel = np.ones((5,5),np.float32)/25
img2gray = cv2.filter2D(img2gray,-1,kernel)

# threshold the image and extract contours
ret,thresh = cv2.threshold(img2gray,250,255,cv2.THRESH_BINARY_INV)
im2,contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)


# find …
Run Code Online (Sandbox Code Playgroud)

python opencv contour

13
推荐指数
1
解决办法
3万
查看次数

错误:将“xxx”作为“this”参数传递会丢弃限定符

谁能告诉我为什么下面的代码:

#include <iostream>
using namespace std;

class Test {
    int a, b, c;
public:
    Test() : a(1), b(2), c(3) {}
    const void print() {cout << a << b << c;}
    int sum() {return (a+b+c);}
};

const Test& f(const Test& test) {
    test.print();
    // cout << test.sum();
    return test;
}

main() {
    Test x;
    cout << "2: ";
    y = f(x);
    cout << endl;
}
Run Code Online (Sandbox Code Playgroud)

给出编译错误

“错误:将‘const Test’作为‘this’参数传递会丢弃限定符”

?

我的print()方法是const,这是我所理解的必要方法。对我来说,(注释掉的)sum()方法f()应该给出错误,但不是print()方法。如果有人能向我指出我的误解所在,那就太好了。

c++

4
推荐指数
2
解决办法
5466
查看次数

`var in array`和`array.indexOf(var)`有什么区别?

我试图在JS中解决数组问题.我的问题是; 以下两个测试是否相同?

var test = 2;
console.log(test in [1,2,3]);
console.log([1,2,3].indexOf(test) != -1);
Run Code Online (Sandbox Code Playgroud)

他们似乎是,但后来回答像这样与本书我读建议你不能做in一个阵列上,只有一个对象.寻找清晰度.必须有一个人们使用的原因.indexOf(x)(我假设是线性时间)而不是in(我假设是恒定时间).

javascript arrays

3
推荐指数
1
解决办法
132
查看次数

从Vector :: Vector基类继承

我试图了解C++中的继承.我试图创建一个vector::vector只接受string指针的子类.

我对指针和引用的控制无疑是微弱的,但我不能为我的生活找出为什么以下代码将每个设置string为相同的地址:

#include <iostream>
#include <vector>
using namespace std;

class StringVector : public vector<void*> {
public:
    void push_back(const string* str) {
        vector::push_back(&str);
    }
    string* operator[](int pos) {
        return (string*)vector::operator[](pos);
    }
};

main() {
    StringVector sv;
    string str1 = "hello";
    string str2 = "world";
    sv.push_back(&str1);
    sv.push_back(&str2);
    for (int i = 0; i < 2; i++)
        cout << sv[i] << " ";
}
Run Code Online (Sandbox Code Playgroud)

当我按原样跑时,我得到:

0xffffcb68 0xffffcb68
Run Code Online (Sandbox Code Playgroud)

即同一地址.

当我试图尊重sv[i](即*sv[i]); 我得到一个例外.谁能告诉我哪里出错了?

c++ vector

0
推荐指数
1
解决办法
438
查看次数

标签 统计

c++ ×2

arrays ×1

contour ×1

javascript ×1

opencv ×1

python ×1

vector ×1