- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想在控制点上绘制曲线
,这样我就可以移动曲线来改变颜色,下面是代码,从a very well explained answer获得帮助
const int N=5; // number of control points (must be >= 4)
float ctrl[N]= // control points y values initiated with linear function y=x
{ // x value is index*1.0/(N-1)
0.00,
0.25,
0.50,
0.75,
1.00,
};
float correction(float col,float *ctrl,int n)
{
float di=1.0/float(n-1);
int i0,i1,i2,i3;
float t,tt,ttt;
float a0,a1,a2,a3,d1,d2;
// find start control point
col*=float(n-1);
i1=col; col-=i1;
i0=i1-1;
i2=i1+1; if (i2>=n) i2=n-1;
i3=i1+2;
// compute interpolation coefficients
if (i0>=0) d1=0.5*(ctrl[i2]-ctrl[i0]); else d1=ctrl[i2]-ctrl[i1];
if (i3< n) d2=0.5*(ctrl[i3]-ctrl[i1]); else d2=ctrl[i2]-ctrl[i1];
a0=ctrl[i1];
a1=d1;
a2=(3.0*(ctrl[i2]-ctrl[i1]))-(2.0*d1)-d2;
a3=d1+d2+(2.0*(-ctrl[i2]+ctrl[i1]));
// now interpolate new colro intensity
t=col; tt=t*t; ttt=tt*t;
t=a0+(a1*t)+(a2*tt)+(a3*ttt);
return t;
}
int main (int argc, const char** argv)
{
Mat input = imread ("E:\\img2.jpg");
Mat input1 = input;
for(int i=0 ; i<input1.rows ; i++)
{
for (int p=0;p<input1.cols;p++)
{
input1.at<cv::Vec3b>(i,p)[0] = 255*correction(input1.at<cv::Vec3b>(i,p)[0]/255.0,ctrl,N); //B
input1.at<cv::Vec3b>(i,p)[1] = 255*correction(input1.at<cv::Vec3b>(i,p)[1]/255.0,ctrl,N); //G
input1.at<cv::Vec3b>(i,p)[2] = 255*correction(input1.at<cv::Vec3b>(i,p)[2]/255.0,ctrl,N); //R
}
}
imshow("image" , input);
waitKey();
但所需的输出就像曲线 程序正在运行,但曲线并没有像下面那样在我的屏幕上绘制,如何使用 opencv 或其他方法绘制它们
这样这些线就可以播放了它们特定颜色在图像中的作用及其值,如下图 right one
是原始图像,左图是通过 cuves
更改值的结果,下面的曲线是每个 B、G、R 的控制点
最佳答案
此代码草图允许您使用鼠标编辑样条曲线,它使用来自 this link 的文件(附加到您的项目:overhauser.cpp overhauser.hpp 和 vec3.hpp):
鼠标左键添加/移动一个点,右键删除。
#include <iostream>
#include <vector>
#include <stdio.h>
#include <functional>
#include <algorithm>
#include <numeric>
#include <cstddef>
#include "opencv2/opencv.hpp"
#include <iostream>
#include <fstream>
#include "overhauser.hpp"
using namespace std;
using namespace cv;
Mat result;
Mat Img;
int current_color=0;
vector<cv::Point2f> pts_red;
vector<cv::Point2f> pts_green;
vector<cv::Point2f> pts_blue;
Mat curvesImg;
int selectedPt=-1;
CRSpline* spline_red = 0;
CRSpline* spline_green = 0;
CRSpline* spline_blue = 0;
unsigned char LUT_RED[256];
unsigned char LUT_GREEN[256];
unsigned char LUT_BLUE[256];
// comparison function:
bool mycomp (Point2f p1, Point2f p2)
{
return p1.x<p2.x;
}
float dist(Point2f p1,Point2f p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int findNEarestPt(Point2f pt, float maxDist)
{
vector<Point2f> current_pts_set;
current_color=0;
if(pt.x>255 && pt.x<512)
{
current_color=1;
}
if(pt.x>=512)
{
current_color=2;
}
float ptx=pt.x;
switch(current_color)
{
case 0:
current_pts_set=pts_red;
break;
case 1:
current_pts_set=pts_green;
pt.x-=255;
break;
case 2:
current_pts_set=pts_blue;
pt.x-=511;
break;
}
float minDist=FLT_MAX;
int ind=-1;
for(int i=0;i<current_pts_set.size();++i)
{
float d=dist(pt,current_pts_set[i]);
if(minDist>d)
{
ind=i;
minDist=d;
}
}
if(minDist>maxDist)
{
ind=-1;
}
return ind;
}
float F(float t,float x, CRSpline* spline)
{
vec3 rv = spline->GetInterpolatedSplinePoint(t);
return x-rv.x;
}
float solveForX(float x,CRSpline* slpine)
{
float a=-1.0f,b=1.0,c,e=1e-2;
c=(a+b)/2;
while( (fabs(b-a)>e) && (F(c,x,slpine)!=0) )
{
if (F(a,x,slpine)*F(c,x,slpine)<0)
{
b=c;
}
else
{
a=c;
}
c=(a+b)/2;
}
return c;
}
int ind=-1;
void mouseHandler(int event, int x, int y, int flags, void* param)
{
Point2f m;
m.x=x;
m.y=y;
curvesImg=Scalar(0,0,0);
switch (event)
{
case cv::EVENT_RBUTTONDOWN:
ind=findNEarestPt(m,5);
if (ind==-1)
{
}else
{
switch(current_color)
{
case 0:
pts_red.erase(pts_red.begin()+ind);
break;
case 1:
pts_green.erase(pts_green.begin()+ind);
break;
case 2:
pts_blue.erase(pts_blue.begin()+ind);
break;
}
ind=-1;
}
break;
case cv::EVENT_LBUTTONDOWN:
ind=findNEarestPt(m,5);
if (ind==-1)
{
switch(current_color)
{
case 0:
pts_red.push_back(m);
selectedPt=pts_red.size()-1;
break;
case 1:
pts_green.push_back(Point2f(m.x-255.0,m.y));
selectedPt=pts_green.size()-1;
break;
case 2:
pts_blue.push_back(Point2f(m.x-511,m.y));
selectedPt=pts_blue.size()-1;
break;
}
}else
{
selectedPt=ind;
}
break;
case cv::EVENT_MOUSEMOVE:
if(ind!=-1)
{
switch(current_color)
{
case 0:
pts_red[selectedPt].x=m.x;
pts_red[selectedPt].y=m.y;
break;
case 1:
pts_green[selectedPt].x=m.x-255;
pts_green[selectedPt].y=m.y;
break;
case 2:
pts_blue[selectedPt].x=m.x-511;
pts_blue[selectedPt].y=m.y;
break;
}
}
break;
case cv::EVENT_LBUTTONUP:
ind=-1;
break;
}
std::sort(pts_red.begin(),pts_red.end(),mycomp);
if(pts_red.size()>0)
{
pts_red[pts_red.size()-1].x=255;
pts_red[0].x=0;
}
std::sort(pts_green.begin(),pts_green.end(),mycomp);
if(pts_green.size()>0)
{
pts_green[pts_green.size()-1].x=255;
pts_green[0].x=0;
}
std::sort(pts_blue.begin(),pts_blue.end(),mycomp);
if(pts_blue.size()>0)
{
pts_blue[pts_blue.size()-1].x=255;
pts_blue[0].x=0;
}
for(int i=0;i<pts_red.size();++i)
{
circle(curvesImg,pts_red[i],5,Scalar(0,0,255),-1,CV_AA);
}
for(int i=0;i<pts_green.size();++i)
{
circle(curvesImg,Point2f(pts_green[i].x+255,pts_green[i].y),5,Scalar(0,255,0),-1,CV_AA);
}
for(int i=0;i<pts_blue.size();++i)
{
circle(curvesImg,Point2f(pts_blue[i].x+511,pts_blue[i].y),5,Scalar(255,0,0),-1,CV_AA);
}
if (spline_red) {delete spline_red;}
spline_red = new CRSpline();
if (spline_green) {delete spline_green;}
spline_green = new CRSpline();
if (spline_blue) {delete spline_blue;}
spline_blue = new CRSpline();
for (int i=0;i<pts_red.size();++i)
{
vec3 v(pts_red[i].x,pts_red[i].y,0);
spline_red->AddSplinePoint(v);
}
for (int i=0;i<pts_green.size();++i)
{
vec3 v(pts_green[i].x,pts_green[i].y,0);
spline_green->AddSplinePoint(v);
}
for (int i=0;i<pts_blue.size();++i)
{
vec3 v(pts_blue[i].x,pts_blue[i].y,0);
spline_blue->AddSplinePoint(v);
}
vec3 rv_last(0,0,0);
if(pts_red.size()>2)
{
for(int i=0;i<256;++i)
{
float t=solveForX(i,spline_red);
vec3 rv = spline_red->GetInterpolatedSplinePoint(t);
if(rv.y>255){rv.y=255;}
if(rv.y<0){rv.y=0;}
unsigned char I=(unsigned char)(rv.y);
LUT_RED[i]=255-I;
if(i>0)
{
line(curvesImg,Point(rv.x,rv.y),Point(rv_last.x,rv_last.y),Scalar(0,0,255),1);
}
rv_last=rv;
}
}
rv_last=vec3(0,0,0);
if(pts_green.size()>2)
{
for(int i=0;i<256;++i)
{
float t=solveForX(i,spline_green);
vec3 rv = spline_green->GetInterpolatedSplinePoint(t);
if(rv.y>255){rv.y=255;}
if(rv.y<0){rv.y=0;}
unsigned char I=(unsigned char)(rv.y);
LUT_GREEN[i]=255-I;
if(i>0)
{
line(curvesImg,Point(rv.x+255,rv.y),Point(rv_last.x+255,rv_last.y),Scalar(0,255,0),1);
}
rv_last=rv;
}
}
rv_last=vec3(0,0,0);
if(pts_blue.size()>2)
{
for(int i=0;i<256;++i)
{
float t=solveForX(i,spline_blue);
vec3 rv = spline_blue->GetInterpolatedSplinePoint(t);
if(rv.y>255){rv.y=255;}
if(rv.y<0){rv.y=0;}
unsigned char I=(unsigned char)(rv.y);
LUT_BLUE[i]=255-I;
if(i>0)
{
line(curvesImg,Point(rv.x+511,rv.y),Point(rv_last.x+511,rv_last.y),Scalar(255,0,0),1);
}
rv_last=rv;
}
}
int cur_col=0;
if(m.x>255 && m.x<512)
{
cur_col=1;
}
if(m.x>=512)
{
cur_col=2;
}
Scalar col;
switch(cur_col)
{
case 0:
col=Scalar(0,0,255);
break;
case 1:
col=Scalar(0,255,0);
break;
case 2:
col=Scalar(255,0,0);
break;
}
line(curvesImg,Point(0,m.y),Point(curvesImg.cols,m.y),col,1);
line(curvesImg,Point(m.x,0),Point(m.x,curvesImg.rows),col,1);
imshow("Correction curves",curvesImg);
vector<Mat> ch;
cv::split(Img,ch);
LUT(ch[0],Mat(256,1,CV_8UC1,LUT_BLUE),ch[0]);
LUT(ch[2],Mat(256,1,CV_8UC1,LUT_RED),ch[2]);
LUT(ch[1],Mat(256,1,CV_8UC1,LUT_GREEN),ch[1]);
cv::merge(ch,result);
imshow("Transformed",result);
}
// ---------------------------------
//
// ---------------------------------
//==============================================================================
int main( int argc, char** argv )
{
for (int i=0;i<256;++i)
{
LUT_RED[i]=i;
LUT_GREEN[i]=i;
LUT_BLUE[i]=i;
}
namedWindow("Image");
namedWindow("Correction curves");
namedWindow("Transformed");
Img=imread("D:\\ImagesForTest\\lena.jpg",1);
imshow("Image",Img);
curvesImg=Mat::zeros(256,768,CV_8UC3);
setMouseCallback("Correction curves", mouseHandler, NULL);
waitKey(0);
getchar();
}
关于opencv - 如何使用opencv在控制点上绘制曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23641208/
我正在尝试创建 treasury yield curve 的图表比较两个不同日期的汇率。我很难将两条曲线组合起来并创建一个干净的图形。 我的问题:如何将两条 yield 曲线绘制在一起, yield
我在 R 平台中使用 randomForest 包进行分类任务。 rf_object<-randomForest(data_matrix, label_factor, cutoff=c(k,1-k))
我的设计师给我设计了这个设计,但我不知道如何最好地处理图像上方和下方的曲线。 我考虑过 clip-path 但不知道如何 flex 它。如果可以的话,我不想使用图像。 最佳答案 您可以使用 borde
我正在使用 Canvas 中的笔触和路径来制作两条线,我希望它们像波浪效果一样弯曲。而不是在 Photoshop 中创建实际图像来实现此目的。 谁能帮忙得到如下图所示的曲线? 我还想在末端实现圆 An
我正在尝试开发一种可以处理图像骨架的路径/曲线的代码。我想要一个来自两点之间骨架的点 vector 。 这段代码加了点就结束了,没找到解决办法。 #include "opencv2/highgui/
现在需要帮助。我可以用MKPolyline和MKPolylineView画线,但是如何在MKMapView上的两个坐标之间画弧线或曲线呢?非常感谢。 最佳答案 在回答问题之前,重要的是要提到 MKOv
我正在尝试应用 sklearn 的想法 ROC extension to multiclass到我的数据集。我的每类 ROC 曲线看起来都找到了一条直线,取消显示曲线波动的 sklearn 示例。 我
我有以下概念问题,我无法理解。 以下是调查数据示例,其中我有一个时间列,指示某人需要多长时间才能回答某个问题。 现在,我感兴趣的是清洁量将如何根据此阈值发生变化,即如果我增加阈值会发生什么,如果我降低
如何为使用视频的对象检测应用绘制每个窗口的误报率与未命中率(或误报概率)和 ROC(接收器操作曲线)的图表?如何确定误报和命中的数量?一个例子是很有用。 最佳答案 它很简单。将所有真正 (H0) 值存
我正在尝试绘制随机森林分类的 ROC 曲线。绘图有效,但我认为我绘制了错误的数据,因为生成的绘图只有一个点(准确性)。 这是我使用的代码: set.seed(55) data.controls <
我有如下两个模型: library(mlbench) data(Sonar) library(caret) set.seed(998) my_data <- Sonar fitControl <-
是否可以仅通过查看其 ROC 曲线来了解分类器是否过度拟合?我看到如果它的 AUC 太高(例如 98%)可能会过度拟合,但这也可能意味着分类器非常好。有没有办法区分这两种情况? 最佳答案 简短的回答:
我正在 JavaFX 中创建一个图形,它应该由有向边连接。最好是双三次曲线。有谁知道如何添加箭头? 箭头当然应该根据曲线的末端进行旋转。 这是一个没有箭头的简单示例: import javafx.ap
我需要对我正在尝试的技术进行一些说明。我正在尝试将一个实体从 A 点移动到 B 点,但我不希望该实体沿直线移动。 例如,如果实体位于 x: 0, y:0 并且我想到达点 x:50, y: 0,我希望实
我试图在曲线下方绘制阴影区域,但阴影区域位于曲线上方。谁能告诉我我的代码有什么问题? x=seq(0,30) y1=exp(-0.1*x) plot(x,y1,type="l",lwd=2,col="
我需要对我正在尝试的技术进行一些说明。我正在尝试将一个实体从 A 点移动到 B 点,但我不希望该实体沿直线移动。 例如,如果实体位于 x: 0, y:0 并且我想到达点 x:50, y: 0,我希望实
我有一个如下所示的模型: library(mlbench) data(Sonar) library(caret) set.seed(998) my_data <- Sonar fitControl <
有没有办法从pyspark中的Spark ML获取ROC曲线上的点?在文档中,我看到了一个 Scala 的例子,但不是 python:https://spark.apache.org/docs/2.1
我正在尝试使用Local Outlier Factor (LOF)算法,并想绘制 ROC 曲线。问题是,scikit-learn 提供的库不会为每个预测生成分数。 那么,有什么办法可以解决这个问题吗?
我目前正在使用 GDI+ 绘制折线图,并使用 Graphics.DrawCurve 来平滑线条。问题是曲线并不总是与我输入的点匹配,这使得曲线在某些点上超出了图形框架,如下所示(红色是 Graph
我是一名优秀的程序员,十分优秀!