我有两个变量的函数f(x,y),其中我需要知道它与零交叉的曲线的位置.ContourPlot非常有效地做到了这一点(即:它使用巧妙的多网格方法,而不仅仅是一个强力细粒度扫描),但只是给了我一个情节.我想有一组值{x,y}(具有一些指定的分辨率)或者可能有一些插值函数,它允许我访问这些轮廓的位置.
想过从ContourPlot的FullForm中提取这个,但这似乎有点像黑客.有更好的方法吗?
我试图使用提取图像中轮廓的近似值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) 我想输出一个蓝色的手,但得到不正确的输出.我已经包含输入图片,错误的输出图片和下面的代码.
我认为下面的代码不会填满整个图像,因为图像在右边界尚未关闭.
如何关闭形状并正确填充蓝色?

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
void drawStuff();
void showInputWindow();
void showCannyWindow();
void showContourWindow();
int thresh = 40;
int max_thresh = 120;
Mat img_rgb,img_gray,img_bw,canny_output,drawing;
int main(){
img_rgb = imread("qq.jpg");
blur( img_rgb, img_rgb, Size(3,3) );
cvtColor(img_rgb,img_gray,CV_RGB2GRAY);
showInputWindow();
drawStuff();
cv::waitKey(0);
}
void drawStuff(){
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Canny( img_gray, canny_output, thresh, thresh*2, 3 );
cv::dilate(canny_output, canny_output, cv::Mat(), cv::Point(-1,-1));
showCannyWindow();
findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, …Run Code Online (Sandbox Code Playgroud) 可能重复:
了解openCV 2.4中感兴趣的区域
我想从图像(Mat格式)获得一个子图像(由下面的红框限定).我该怎么做呢?

这是我目前的进展:
include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat imgray, thresh;
vector<vector<Point> >contours;
vector<Point> cnt;
vector<Vec4i> hierarchy;
Point leftmost;
Mat im = imread("igoy1.jpg");
cvtColor(im, imgray, COLOR_BGR2GRAY);
threshold(imgray, thresh, 127, 255, 0);
findContours(thresh, contours, hierarchy, RETR_TREE,CHAIN_APPROX_SIMPLE);
}
Run Code Online (Sandbox Code Playgroud) 是否可以在对数刻度中设置颜色条的级别,如下图所示?

以下是可以实现的示例代码:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
delta = 0.025
x = y = np.arange(0, 3.01, delta)
X, Y = np.meshgrid(x, y)
Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 1e6 * (Z1* Z2)
fig=plt.figure()
ax1 = fig.add_subplot(111)
lvls = np.logspace(0,4,20)
CF = ax1.contourf(X,Y,Z,
norm = LogNorm(),
levels = lvls
)
CS = ax1.contour(X,Y,Z,
norm = LogNorm(),
colors = 'k',
levels = lvls …Run Code Online (Sandbox Code Playgroud) 我有一个庞大的散点图(~100,000点),我在matplotlib中生成.每个点在此x/y空间中都有一个位置,我想生成包含总点数的某些百分位数的轮廓.
matplotlib中是否有一个函数可以执行此操作?我已经研究过contour(),但是我必须用这种方式编写自己的函数.
谢谢!
我正在使用OpenCV(Canny + findCountours)来查找对象的外部轮廓.绘制的曲线通常几乎(但不是完全)是闭合的.我想关闭它 - 找到它所包含的区域.
我该怎么做呢?
事情考虑:
我有以下图表,包括几行:

现在,我想标记情节中的所有线条.但是,legend()在框中将所有标签一起使用,这使得情节难以解释.我想要的是使用内联标签.我的理想输出将使用类似下面的matplotlib等高线图,但带有行的文本标签而不是数字:

我无法在matplotlib文档中找到如何执行此操作.有没有办法实现这个目标?如果没有,我可以用什么其他软件来生成这种类型的情节?
该matplotlib.pyplot.contour()函数需要3个输入数组X,Y和Z.
数组X并Y指定点的x坐标和y坐标,同时Z指定在点处评估的感兴趣函数的对应值.
据我所知,np.meshgrid()可以很容易地生成作为参数的数组contour():
X = np.arange(0,5,0.01)
Y = np.arange(0,3,0.01)
X_grid, Y_grid = np.meshgrid(X,Y)
Z_grid = X_grid**2 + Y_grid**2
plt.contour(X_grid, Y_grid, Z_grid) # Works fine
Run Code Online (Sandbox Code Playgroud)
这很好用.方便的是,这也很好:
plt.contour(X, Y, Z_grid) # Works fine too
Run Code Online (Sandbox Code Playgroud)
但是,为什么Z输入需要是2D阵列?
为什么类似以下内容不允许,即使它指定所有相同的数据对齐?
plt.contour(X_grid.ravel(), Y_grid.ravel(), Z_grid.ravel()) # Disallowed
Run Code Online (Sandbox Code Playgroud)
另外,只 Z指定了什么是语义(没有相应的X和Y)?
我有一个坐标变换的二维地图.每个点的数据是原始坐标系中的轴上角,从0到360.我正在尝试使用pyplot.contour绘制恒定角度的线,例如45度.轮廓沿着两极之间的45度线出现,但是沿着0/360不连续性连接两个极的轮廓还有一个附加部分.这是一个非常锯齿状的丑陋线,因为它基本上只跟踪像素,一边接近0,另一边接近360.
示例:这是使用全彩色地图的图像:

您可以在左侧看到蓝色/红色曲线的不连续性.一侧是360度,另一侧是0度.在绘制轮廓时,我得到:

请注意,所有轮廓都连接两个极点,但即使我没有绘制0度轮廓,所有其他轮廓都遵循0度不连续性(因为pyplot认为一边是0而另一边是360,必须有所有其他角度之间).
生成此数据的代码:
import numpy as np
import matplotlib.pyplot as plt
jgal = np.array( [[-0.054875539726,-0.873437108010,-0.483834985808],\
[0.494109453312,-0.444829589425, 0.746982251810],\
[-0.867666135858,-0.198076386122, 0.455983795705]])
def s2v3(rra, rdec, r):
pos0 = r * np.cos(rra) * np.cos(rdec)
pos1 = r * np.sin(rra) * np.cos(rdec)
pos2 = r * np.sin(rdec)
return np.array([pos0, pos1, pos2])
def v2s3(pos):
x = pos[0]
y = pos[1]
z = pos[2]
if np.isscalar(x): x, y, z = np.array([x]), np.array([y]), np.array([z])
rra = np.arctan2(y, x)
low = np.where(rra < 0.0)
high = np.where(rra …Run Code Online (Sandbox Code Playgroud)