我有#include <opencv2\opencv.hpp>
int main()
{
cv::Mat3b star = cv::imread("path/to/image");
cv::imshow("Original", star);
cv::Mat3b hsv;
cv::cvtColor(star, hsv, cv::COLOR_BGR2HSV);
cv::Mat1b mask;
cv::inRange(hsv, cv::Scalar(29, 220, 220), cv::Scalar(30, 255, 255), mask);
cv::imshow("mask", mask);
// Change to 'false' to see how to use the 'cv::mask' approach
if (true)
{
double blue, green, red = 0.0;
int counter = 0;
for (int r = 0; r < star.rows; r++)
{
for (int c = 0; c < star.cols; c++)
{
if (mask(r, c) > 0)
{
++counter;
blue += star(r, c)[0];
green += star(r, c)[1];
red += star(r, c)[2];
}
}
}
// Avoid division by 0
if (counter > 0)
{
blue /= counter;
green /= counter;
red /= counter;
}
std::cout << "$$ Red(R), Green(G), Blue(B) $$" << " \n\n";
std::cout << "avg_R: " << red << " \n";
std::cout << "avg_G: " << green << " \n";
std::cout << "avg_B: " << blue << " \n\n";
}
else
{
cv::Scalar mean_value = cv::mean(star, mask);
double blue = mean_value[0];
double green = mean_value[1];
double red = mean_value[2];
std::cout << "$$ Red(R), Green(G), Blue(B) $$" << " \n\n";
std::cout << "avg_R: " << red << " \n"; // red value
std::cout << "avg_G: " << green << " \n"; // green value
std::cout << "avg_B: " << blue << " \n\n"; // blue value
}
cv::waitKey();
}
函数,目的是选择该值是否在数组内已经存在。如果不存在,此函数会将值输入到updateLocation
中,然后将其放在array
上,反之亦然。
但是当我尝试单击它时,出现了这样的错误:
未捕获的TypeError:无法读取未定义的属性“ includes”
这是我写的代码:
state
我认为这个错误实际上是没有意义的,因为以前有一个功能几乎相同,但是可以正常工作。
updateLocation(e) {
let locationArray = this.state.checked.location;
let value = parseInt(e.target.value);
//v this is the error comes from v
if (locationArray.includes(value)) {
locationArray.splice(locationArray.indexOf(value), 1);
} else {
locationArray.push(value)
}
this.setState({
checked: {
location: locationArray
}
})
}
状态
updateCategory(e) {
let categoryArray = this.state.checked.categories;
let value = parseInt(e.target.value);
//this one works fine even It looks the same like updateLocation()
if (categoryArray.includes(value)) {
categoryArray.splice(categoryArray.indexOf(value), 1);
} else {
categoryArray.push(value)
}
this.setState({
checked: {
categories: categoryArray
}
})
}
constructor(props) {
super(props);
this.state = {
jobs : [],
category: [],
location: [],
loading : true,
count : 8,
checked : {
categories: [],
location : []
}
}
}
函数位于更新updateCategory
上方
这是称为updateLocation
的代码
updateLocation
我尝试<div className="card-body">
{state.location.map((item, i) => (
<div className="checkbox" key={i}>
<input type="checkbox" value={item.id} onChange={e => this.updateLocation(e)} checked={state.checked.categories.includes(item.id)}/> {item.city_name}
</div>
)}
<a onClick={e => this.viewAllLocation(e)} id="view-all-loc">View more >>></a>
<div id="show-loading-loc" style={{display: 'none'}}>Loading...</div>
</div>
,但奇怪的是,没有出现错误,只是控制台中console.log(locationArray.includes(value))
的结果是locationArray.includes(value)
我发现了主要问题,false
函数中的所有内容都可以正常工作,但是当updateLocation
将存储在locationArray
中时,它会出错。但这仍然没有意义,因为我们已经有state
的状态
答案 0 :(得分:5)
问题在于您要为checked
对象分配一个没有其他属性的新实例。
this.setState({
checked: {
categories: categoryArray
}
})
此setState
正在从location
删除this.state.checked
属性。
您应该使用:
this.setState({
checked: {
...this.state.checked,
categories: categoryArray
}
})