在第一个程序中加入active object使球运动起来

博客介绍了在Symbian C++中使用活动对象进行定时任务的方法。先定义继承于CActive的CPeriodic活动对象,通过Start方法设置延迟、间隔和回调方法启动线程,还给出停止线程的方法,并附上了具体实例代码,包含回调方法和重绘操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.先定义一个CPeriodic* iTimer;活动对象,CPeriodic 继承于 CActive ,在symbian c++中建议使用CActive而不使用RTimer。
2.iTimer->Start(TTimeIntervalMicroSeconds32 aDelay,TTimeIntervalMicroSeconds32 anInterval,TCallBack aCallBack);TTimeIntervalMicroSeconds32 从第一次事件的产生到活动对象的star()的延迟时间,以毫秒计算;TTimeIntervalMicroSeconds32 初始化后到事件的产生的间隔,以毫秒计算;TCallBack 封装了一个call-back方法,TCallBack(TInt (*aFunction)(TAny* aPtr),TAny* aPtr);TInt (*aFunction)(TAny* aPtr)传入某个类的静态方法,该方法要传入TAny*即任何对象,返回Tint类型。TAny* aPtr刚才那个静态方法所在的类,可以为NULL;线程通过此方法被启动,并在一定的时间间隔调用这个静态方法。
3.停止线程,iTimer->cancel();
实例:
CPeriodic* iPeriodicTimer; //定义活动对象

 if(!iPeriodicTimer->IsActive()){
  iPeriodicTimer->Start(10,10,TCallBack(CMap2Container::Period,this));//启动线程并调用static Period方法
 }

 TInt CMap2Container::Period(TAny * aPtr) //具体的period方法
{
 ((CMap2Container*)aPtr)->DoPeriodTask();
    //returning a value of TRUE indicates the callback should be done again
 return TRUE;
}

 void CMap2Container::DoPeriodTask(){
 iTPoint.iX = iX++;
 iTPoint.iY = iY ++;
 DrawNow();//调用draw方法,但不能直接用draw
// CWindowGc& gc = SystemGc();
// gc.Activate(*DrawableWindow());
// UpdateScreen();
// gc.Deactivate(); //或者这个方法重绘
}
 void CMap2Container::UpdateScreen() const{
  CWindowGc& gc = SystemGc();

 gc.BitBlt(TPoint(0,0),iMap);
 
gc.BitBltMasked(iTPoint,iBall,TRect(TPoint(0,0),iBall->SizeInPixels)),iBallMask,ETrue);

 
 } 
void draw(){
    UpdateScreen();
}

``` import cv2 import numpy as np import openpyxl def process_image(image, wb, sheet, frame_count, scale_percent=0.35): try: # 图像缩放 height, width = image.shape[:2] new_width = int(width * scale_percent) new_height = int(height * scale_percent) resized = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA) # 转换为HSV颜色空间 hsv = cv2.cvtColor(resized, cv2.COLOR_BGR2HSV) # 定义红色范围(两个区间) lower_red1 = np.array([0, 120, 70]) upper_red1 = np.array([10, 255, 255]) lower_red2 = np.array([160, 120, 70]) upper_red2 = np.array([180, 255, 255]) # 创建掩膜 mask1 = cv2.inRange(hsv, lower_red1, upper_red1) mask2 = cv2.inRange(hsv, lower_red2, upper_red2) mask = cv2.bitwise_or(mask1, mask2) # 形态学操作 kernel = np.ones((5, 5), np.uint8) mask = cv2.erode(mask, kernel, iterations=1) mask = cv2.dilate(mask, kernel, iterations=2) # 查找轮廓 contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if contours: # 取最大轮廓 max_contour = max(contours, key=cv2.contourArea) # 获取边界框 x, y, w, h = cv2.boundingRect(max_contour) # 输出坐标到Excel row = frame_count + 2 # 从第二行开始(保留表头) sheet.cell(row=row, column=1).value = frame_count sheet.cell(row=row, column=2).value = f"({x}, {y})" sheet.cell(row=row, column=3).value = f"({x + w}, {y})" sheet.cell(row=row, column=4).value = f"({x}, {y + h})" sheet.cell(row=row, column=5).value = f"({x + w}, {y + h})" # 绘制检测框 cv2.rectangle(resized, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(resized, f"Tracking", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) cv2.imshow('Detection', resized) cv2.waitKey(1) except Exception as e: print("Processing error:", e) def process_video(video_path, scale_percent=0.35): cap = cv2.VideoCapture(video_path) frame_count = 0 # 创建Excel文件 wb = openpyxl.Workbook() sheet = wb.active sheet.title = "Red Object Tracking" sheet['A1'] = "Frame" sheet['B1'] = "Top Left" sheet['C1'] = "Top Right" sheet['D1'] = "Bottom Left" sheet['E1'] = "Bottom Right" while cap.isOpened(): ret, frame = cap.read() if not ret: break process_image(frame, wb, sheet, frame_count, scale_percent) frame_count += 1 wb.save("red_object_coordinates.xlsx") cap.release() cv2.destroyAllWindows() print(f"Processed {frame_count} frames") # 使用示例 video_path = "1.1.mp4" process_video(video_path)```为什么得到的坐标值是固定的
最新发布
03-27
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值