Основные шаги как @Miki,
- читать источник
- threshed
- find minAreaRect
- warp (g9)
- [gg] [gg] Хотя код в Python:
#!/usr/bin/python3 # 2018.01.16 01:11:49 CST # 2018.01.16 01:55:01 CST import cv2 import numpy as np ## (1) read img = cv2.imread("img02.jpg") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ## (2) threshold th, threshed = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU) ## (3) minAreaRect on the nozeros pts = cv2.findNonZero(threshed) ret = cv2.minAreaRect(pts) (cx,cy), (w,h), ang = ret if w>h: w,h = h,w ang += 90 ## (4) Find rotated matrix, do rotation M = cv2.getRotationMatrix2D((cx,cy), ang, 1.0) rotated = cv2.warpAffine(threshed, M, (img.shape[1], img.shape[0])) ## (5) find and draw the upper and lower boundary of each lines hist = cv2.reduce(rotated,1, cv2.REDUCE_AVG).reshape(-1) th = 2 H,W = img.shape[:2] uppers = [y for y in range(H-1) if hist[y]<=th and hist[y+1]>th] lowers = [y for y in range(H-1) if hist[y]>th and hist[y+1]<=th] rotated = cv2.cvtColor(rotated, cv2.COLOR_GRAY2BGR) for y in uppers: cv2.line(rotated, (0,y), (W, y), (255,0,0), 1) for y in lowers: cv2.line(rotated, (0,y), (W, y), (0,255,0), 1) cv2.imwrite("result.png", rotated)
Наконец, результат:
Как предложил @ZorgoZ, я добавил всю запись в фильтр действий.
LogActionFilter.cs:
using Sample.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Sample.Filters
{
public class LogActionFilter : ActionFilterAttribute
{
private readonly LogEntities _db = new LogEntities();
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
String originalPath = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).OriginalString;
var inputUser = ConfigurationManager.AppSettings["INPUT_USER"].ToString().ToLower();
Log log = new Log();
{
log.Date = DateTime.Now;
log.URL= originalPath;
log.UserName = inputUser;
_db.UserLogs.Add(log);
_db.Entry(log).State = EntityState.Added;
_db.SaveChanges();
}
base.OnActionExecuting(filterContext);
}
}
}
Но у меня есть еще одна проблема. У меня есть еще один класс контроллера API. когда я пытаюсь получить доступ к API сотрудника через этот http: // localhost: 53037 / api / employee / 4 , мой фильтр действий не выполняется. Может кто-нибудь сказать мне, что я могу упустить или какие изменения мне нужно сделать?
EmployeeController.cs:
using Sample.Filters;
using Sample.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
namespace Sample.Controllers
{
public class EmployeeController : ApiController
{
private readonly LogEntities _db = new LogEntities();
[LogActionFilter]
public IEnumerable<Employee> Get()
{
using(EmployeeDBEntities entities = new EmployeeDBEntities())
{
return entities.Employees.ToList();
}
}
[LogActionFilter]
public Employee Get(int id)
{
using (EmployeeDBEntities entities = new EmployeeDBEntities())
{
return entities.Employees.FirstOrDefault(e => e.ID == id);
}
}
[LogActionFilter]
public void Post([FromBody] Employee employee)
{
using (EmployeeDBEntities entities = new EmployeeDBEntities())
{
entities.Employees.Add(employee);
entities.SaveChanges();
}
}
}
}
Главный контроллер:
using Sample.Filters;
using Sample.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace Sample.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[LogActionFilter]
public ActionResult About(UserLog usr)
{
ViewBag.Message = "Your application description page.";
return View();
}
[LogActionFilter]
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
} [ 1110]